2014-11-21 80 views
0
#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个整数时它刚刚崩溃,我找不到原因。任何想法如何修复它?如何在C程序中查找乘法矩阵的错误

回答

1

你没有为你的矩阵分配内存。在您的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--; 
    } 
}; 
+0

啊。但我需要它的大小mat1x mat1y等等。可能吗? – 2014-11-21 22:42:55

+0

@ user3552292是的。我稍微看一下。 – DevNull 2014-11-21 22:50:38

+0

太棒了,但我试过了。仍然错误。程序甚至不会在struct之后打印一个printf。 – 2014-11-21 22:57:42