2014-11-21 49 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