2014-11-21 1 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--; 
    } 
}; 

여기에 코드가 있습니다. 이제 질문입니다. 임 스레드에서 행렬을 곱하는 프로그램을 만들려고. 문제는 이제 실행하려고 할 때 오류가 발생했습니다. 괜찮아요, 단지 내가 begginng에서 5 ints를 입력하면 그냥 충돌, 그리고 난 왜 찾을 수 없습니다. 어떤 생각을 어떻게 수리 할 수 ​​있습니까?행렬을 곱하는 C 프로그램에서 오류를 찾는 방법

답변

1

행렬에 메모리를 할당하지 않습니다. struct ags 구조에서는 두 개의 long** 항목이 있지만 실제로 사용할 메모리를 할당하지 않습니다. malloc 또는 calloc을 사용하여 동적 메모리 할당을 통해 그렇게해야합니다.

struct ags* strAgs; 

잘못되었습니다. 포인터를 만들었지 만 포인터는 아무 것도 가리 키지 않습니다. 빠른 해결 방법은 코드의 너무 많은 것 다시 쓰기 줄이기 위해 : 당신은 멀티 스레드 행렬 곱셈 신청을 해하려는처럼

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

것 같다. 이것이 숙제 인 것처럼 보이기 때문에 곱셈 논리를 끝내지는 않겠지 만, 리눅스에서 컴파일되고 잘 실행되는 코드의 업데이트 된 버전을 포함하고 동적 메모리 할당과 할당 취소/정리를 처리합니다. 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 등의 크기가 필요합니다. 가능합니까? –

+0

@ user3552292 예. 나는 조금 살펴볼 것이다. – DevNull

+0

위대한,하지만 난 그것을 시도 alrdy. 여전히 오류. 프로그램은 struct 다음에 하나의 printf를 출력하지 않습니다. –

관련 문제