Calculate diagonals of an array using Threads

Asked

Viewed 383 times

4

I need a lot of help. I need to make a program in C in which calculate the diagonals of an array using Threads as stated:

inserir a descrição da imagem aqui

As directed, I must pass the value of diagonal and jump (diagonal change) as parameters, but I do not know what to do with these values.

Follow the code I have been able to do so far (this incomplete because I do not know how to proceed in the function to add the diagonals):

NOTE: I took the functions that generate the file with the values of the matrix and the function that assigns these values to the matrix to make the code cleaner and easier to understand.

typedef struct{
    int diag=0;
    int salto;
}ST;

ST v;

struct param *arg;

void *somarMatriz(void *args){
    ST * in =  (ST *) args;

    int x=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++;
    }

}


int main (){ 

    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");

}

1 answer

0

I made the code down using windows API, it shouldn’t be too hard to move to pthreads. Another (perhaps more efficient) solution 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 indexes of the first element of each diagonal. These values are the indices of the first and first row elements

  • Here is an example of how to Thread Pool in C: https://stackoverflow.com/questions/8357955/windows-api-thread-pool-simple-example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.