Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void aThreadPerElement(int rowA, int colA, int rowB, int colB, int** a, int** b){
- if (colA != rowB) {
- printf("Matrices cannot be multiplied: Incompatible dimensions.\n");
- return;
- }
- struct timeval stop, start;
- int** c = malloc(rowA * sizeof(int*));
- for(int i = 0; i < rowA; i++){
- c[i] = malloc(colB * sizeof(int));
- }
- pthread_t threads[rowA][colB];
- int rc;
- gettimeofday(&start, NULL);
- for (int i = 0; i < rowA; i++) {
- for (int j = 0; j < colB; j++) {
- c[i][j] = 0;
- struct thread_data *tdata = malloc(sizeof(struct thread_data));
- tdata->a = a;
- tdata->b = b;
- tdata->c = c;
- tdata->colA = colA;
- tdata->colB = colB;
- tdata->rowA = rowA;
- tdata->rowB = rowB;
- tdata->i = i;
- tdata->j = j;
- rc = pthread_create(&threads[i][j], NULL, n, (void*)tdata);
- if(rc){
- printf("ERROR; return code from pthread_create() is %d\n", rc);
- exit(-1);
- }
- }
- }
- gettimeofday(&stop, NULL);
- for(int i = 0 ; i < rowA ; ++i){
- for(int j = 0 ; j < colB ; ++j) {
- pthread_join(threads[i][j], NULL);
- }
- }
- printf(RED "Multiplication Finished you can see results in c.txt\n" );
- printf("Seconds taken %lu\n", stop.tv_sec - start.tv_sec);
- printf("Microseconds taken: %lu\n", stop.tv_usec - start.tv_usec);
- printf(RESET);
- FILE *fp;
- fp = fopen("c.txt", "w");
- if (fp == NULL) {
- printf("File not found\n");
- return;
- }
- fprintf(fp, "row=%d col=%d\n", rowA, colB);
- for (int i = 0; i < rowA; i++) {
- for (int j = 0; j < colB; j++) {
- fprintf(fp, "%d ", c[i][j]);
- }
- fprintf(fp, "\n");
- }
- fclose(fp);
- free(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement