gpt4 book ai didi

c - 如何查找矩阵相乘的 C 程序中的错误

转载 作者:行者123 更新时间:2023-11-30 15:27:18 24 4
gpt4 key购买 nike

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

struct ags{
long int **mat1;
long int **mat2;
int *semafor;
int rowsDone;
};

void *thread_multiply(void*);

int main(int args, char** argv){
int mat1x;
int mat1y;
int mat2x;
int mat2y;
int threadsAmount;

printf("Podaj szerokosc pierwszej macierzy: ");
scanf("%i", &mat1x);
printf("Podaj wysokosc pierwszej macierzy: ");
scanf("%i", &mat1y);
printf("Podaj szerokosc drugiej macierzy: ");
scanf("%i", &mat2x);
printf("Podaj wysokosc drugiej macierzy: ");
scanf("%i", &mat2y);
printf("Podaj ilosc watkow: ");
scanf("%i", &threadsAmount);

if(mat1x != mat2y){
printf("Musisz podac odpowiednie macierze!");
return;
}

struct ags* strAgs;

int i;
int j;
for(i = 0; i < mat1y; i++){
for(j = 0; j < mat1x; j++){
strAgs->mat1[i][j] = random();
}
}

for(i = 0; i < mat2y; i++){
for(j = 0; j < mat2x; j++){
strAgs->mat2[i][j] = random();
}
}


for(j = 0; j < mat2x; j++){
strAgs->semafor[j] = 0;
}
strAgs->rowsDone = mat2x;


pthread_t threadsArray[threadsAmount];

for(i = 0; i < threadsAmount; i++){
if(pthread_create(&threadsArray[i], NULL, thread_multiply, (void*) strAgs)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
}

for(i = 0; i < threadsAmount; i++){
if(pthread_join(threadsArray[i], NULL)) {
fprintf(stderr, "Error joining thread\n");
return 1;
}
}
}

void *thread_multiply(void* ptr) {
struct ags* agsStruct = (struct ags*) ptr;
while(agsStruct->rowsDone > 0){
printf("woho\n");
agsStruct->rowsDone--;
}
};

好的,这是代码。现在问题来了。我正在尝试编写一个程序来在线程中乘以矩阵。问题是我在尝试运行它时出现错误。它编译得很好,就在我开始输入 5 个整数时,它就崩溃了,我找不到原因。知道如何修复它吗?

最佳答案

您没有为矩阵分配内存。在您的 struct ags 结构中,您有两个 long** 条目,但您从未实际分配内存供它们使用。您需要使用 malloccalloc 通过动态内存分配来完成此操作。

struct ags* strAgs;

错了。您正在创建一个指针,但没有将其指向任何数据。减少重写过多代码的快速解决方法是:

struct ags actualStruct;
struct ags* strAgs = &actualStruct;

您似乎正在尝试执行多线程矩阵乘法应用程序。我不会完成乘法逻辑,因为这看起来是家庭作业,但我在下面提供了代码的更新版本,它可以在 Linux 上编译和运行良好,并处理动态内存分配和取消分配/清理。 It also uses a constant MAX_VALUE to keep the matrix elements from suffering from integer overflow when your final program starts working.

祝你好运!

代码列表

<小时/>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define MAX_VALUE (10)

struct ags{
long int **mat1;
long int **mat2;
int *semafor;
int rowsDone;
};

void *thread_multiply(void*);

int main(int args, char** argv){
int mat1x;
int mat1y;
int mat2x;
int mat2y;
int threadsAmount;

printf("Podaj szerokosc pierwszej macierzy: ");
scanf("%i", &mat1x);
printf("Podaj wysokosc pierwszej macierzy: ");
scanf("%i", &mat1y);
printf("Podaj szerokosc drugiej macierzy: ");
scanf("%i", &mat2x);
printf("Podaj wysokosc drugiej macierzy: ");
scanf("%i", &mat2y);
printf("Podaj ilosc watkow: ");
scanf("%i", &threadsAmount);

if(mat1x != mat2y){
printf("Musisz podac odpowiednie macierze!");
return;
}
struct ags actualStruct = { 0 };
struct ags* strAgs = &actualStruct;

int i;
int j;
printf("%d %d %d %d %d\n", mat1x, mat1y, mat2x, mat2y, threadsAmount);

/* Dynamic memory allocation */
int iErr = 0;
if (!iErr) {
if ((strAgs->mat1 = calloc(mat1y, sizeof(long int*))) == NULL) {
printf("Memory allocation error!\n");
iErr = 1;
}
}
if (!iErr) {
for (i=0; i<mat1y; i++) {
if ((strAgs->mat1[i] = calloc(mat1x, sizeof(long int))) == NULL) {
printf("Memory allocation error!\n");
iErr = 1;
break;
}
}
}
if (!iErr) {
if ((strAgs->mat2 = calloc(mat2y, sizeof(long int*))) == NULL) {
printf("Memory allocation error!\n");
iErr = 1;
}
}
if (!iErr) {
for (i=0; i<mat2y; i++) {
if ((strAgs->mat2[i] = calloc(mat2x, sizeof(long int))) == NULL) {
printf("Memory allocation error!\n");
iErr = 1;
break;
}
}
}
if (!iErr) {
if ((strAgs->semafor = calloc(mat2x, sizeof(int))) == NULL) {
printf("Memory allocation error!\n");
iErr = 1;
}
}

/* Main logic */
if (!iErr) {
/* Populate the arrays */
for(i = 0; i < mat1y; i++){
for(j = 0; j < mat1x; j++){
strAgs->mat1[i][j] = random() % MAX_VALUE;
}
}
for(i = 0; i < mat2y; i++){
for(j = 0; j < mat2x; j++){
strAgs->mat2[i][j] = random() % MAX_VALUE;
}
}
for(j = 0; j < mat2x; j++){
strAgs->semafor[j] = 0;
}
strAgs->rowsDone = mat2x;

/* Create group of worker threads to perform math operations */
pthread_t threadsArray[threadsAmount];
for(i = 0; i < threadsAmount; i++){
if(pthread_create(&threadsArray[i], NULL, thread_multiply, (void*) strAgs)) {
fprintf(stderr, "Error creating thread\n");
return 1;
}
}

/* Wait for all threads to complete before proceeding */
for(i = 0; i < threadsAmount; i++){
if(pthread_join(threadsArray[i], NULL)) {
fprintf(stderr, "Error joining thread\n");
return 1;
}
}

/* Print out both matrices */
printf("MATRIX 1:\n");
for (i=0; i<mat1y; i++) {
for (j=0; j<mat1x; j++) {
printf("%02ld ", strAgs->mat1[i][j]);
}
printf("\n");
}
printf("MATRIX 2:\n");
for (i=0; i<mat2y; i++) {
for (j=0; j<mat2x; j++) {
printf("%02ld ", strAgs->mat2[i][j]);
}
printf("\n");
}
}


/* Clean up dynamically allocated memory */
for (i=0; i<mat1y; i++) {
free(strAgs->mat1[i]);
}
free(strAgs->mat1);

for (i=0; i<mat2y; i++) {
free(strAgs->mat2[i]);
}
free(strAgs->mat2);

free(strAgs->semafor);

/* Exit application */
return 0;
}

void *thread_multiply(void* ptr) {
struct ags* agsStruct = (struct ags*) ptr;
while(agsStruct->rowsDone > 0){
printf("woho\n");
agsStruct->rowsDone--;
}
};

关于c - 如何查找矩阵相乘的 C 程序中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com