Calculate diagonals of an array using Threads

2

I need huge help. I need to do a C program in which I compute the diagonals of an array using Threads as per the statements:

AsIwastold,Ishouldpassthevalueofthediagonalandthejump(diagonalchange)asparameters,butIdonotknowwhattodowiththosevalues.

ThefollowingisthecodethatIhavebeenabletodosofar(thisisincompletebecauseIdonotknowhowtodothefunctiontoaddthediagonals):

NOTE:Itookthefunctionsthatgeneratethefilewiththevaluesofthearrayandthefunctionthatassignsthesevaluestothearraysothecodebecomescleanerandeasiertounderstand.

typedefstruct{intdiag=0;intsalto;}ST;STv;structparam*arg;void*somarMatriz(void*args){ST*in=(ST*)args;intx=0,y=0;result=(float*)malloc((l+c-1)*sizeof(float));x=0;for(i=;i<l+c-1;i++){result[i]=0;}for(i=l-1;i>=0;i--){x=y;for(j=0;j<c;j++){result[x]+=mat[i][j];x++;}y++;}}intmain(){printf("Entre com o numero de linhas da matriz: \n");
    scanf("%d", &l);

    printf("Entre com o numero de colunas da matriz: \n");
    scanf("%d", &c);

    printf("Entre com o quantidade de Threads desejada:\n");
    scanf("%d", &arg.salto);

    gerarArquivo(l, c);
    gerarMatriz(l, c);

    pthread_t threads[T];
    pthread_attr_t attr;
    int rc;
    long t;
    void *status;

    pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

    for(t=0; t<T; t++){
        rc = pthread_create(&threads[t], NULL, somarMatriz, (void*)&v);
        if (rc){
            printf("ERRO");
            exit(-1);
        }
    }

    pthread_exit((void *)NULL);

    printf("\n\n");

    printf("Resultados:\n");
    for(i=0; i<l+c-1; i++){
        printf("%d\t", result[i]);
  }  

  printf("\n");

}
    
asked by anonymous 09.06.2017 / 21:26

1 answer

0

I made the code down using windows API, it should not be too difficult to move to pthreads. Another solution (perhaps more efficient) would be to use Thread Pool.

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

DWORD WINAPI ThreadFunc(void* data);

typedef struct {
    int x;
    int y;
}PARAM;

int **matrix;
int *result;
int N, M, T;

int main()
{
    scanf_s("%d", &M);
    scanf_s("%d", &N);
    scanf_s("%d", &T);

    int jobAmountLeft = M + N - 2;

    PARAM *param = (PARAM*) calloc(M+N-1, sizeof(PARAM));
    int i;
    for(i = 0; i < M; i++){
         param[i].x = 0;
         param[i].y = i;
    }
    for (; i < M + N - 1; i++) {
        param[i].x = i - M + 1;
        param[i].y = 0;
     }

    result = (int*)calloc(M + N - 1, sizeof(int));

    matrix = (int**)calloc(M, sizeof(int*));
    for(int i = 0; i < M; i++) matrix[i] = (int*) calloc(N, sizeof(int));

    for (int i = 0; i < M; i++) 
        for(int j = 0; j < N; j++) 
            scanf_s("%d", &matrix[i][j]);

    HANDLE *thread = (HANDLE*)calloc(T, sizeof(HANDLE));

    for (int i = 0; i < T; i++) {
        thread[i] = CreateThread(NULL, 0, ThreadFunc, &param[jobAmountLeft], 0, NULL);
        jobAmountLeft--;
        if(jobAmountLeft < 0) break;
     }

    while(jobAmountLeft >= 0){
        for (int i = 0; i < T; i++) {
            WaitForSingleObject(thread[i], 0.01);
            thread[i] = CreateThread(NULL, 0, ThreadFunc, &param[jobAmountLeft], 0, NULL);
            jobAmountLeft--;
            if(jobAmountLeft < 0) break;
         }
    }

    for (int i = 0; i < T; i++) {
        WaitForSingleObject(thread[i], INFINITE);
    }

    for (int i = 0; i < M + N - 1; i++) {
        printf("%d ", result[i]);
    }

    scanf_s("%d",&N);


 }

 DWORD WINAPI ThreadFunc(void* data) {
     PARAM *in = (PARAM*)data;
     int x = in->x;
     int y = in->y;
     int acc = 0;

     for(int i = y, j = x; i < M && j < N; j++, i++){
         acc += matrix[i][j];
     }

     result[M - in->y + in->x - 1] = acc;
     return 0;
 }

For the function I pass the indices of the first element of each diagonal. These values are the indexes of the elements of the first column and the first line

    
08.08.2017 / 20:00