Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Done I think
#include <stdio.h> #include <math.h> #include <sys/mman.h> #include <pthread.h> #include <malloc.h> #include <stdlib.h> int a[18][16]; //first array int b[16][18]; //second array int c[18][18]; // The result //%how to pseudo this code? struct thread_info { int row, col; // The row and column with respect to the element of C being solved for }; typedef struct thread_info thinfo; //%Completes the arthimetic for one element of a product of matrix multiplication. void *compute_C_ij(void *arg) { int i, j, k, sum; //local variables for computation thinfo *info; info = (thinfo *)arg; i = info->row; //is passed by the pthread call. j = info->col; //distinguishes which cij being solved. //%information necessary to be passed is row of a and col of b. and which C value it is. sum = 0; for (k = 0; k < 16; k++) { sum = sum + (a[i][k]*b[k][j]); } c[i][j] = sum; // printf("I = %d J= %d a = %d b = %d C= %d\n", i, j, a[i][j], b[i][j], c[i][j]); //debugging pthread_exit(NULL); } int main(void) { //variables to loops. int i,j,k; k = 0; //for(k=0; k<5; k++){ pthread_t thread[324]; //Thread variables thinfo info[324]; //structs to be passed //} //%initializing matrices a and b for(i=0; i<18; i++){ for(j=0; j<16; j++){ a[i][j] = i+1+j+1; } } for(i=0; i<16; i++){ for(j=0; j<18; j++){ b[i][j] = i+1+(2*(j+1)); } } //Loop to solve for each element of C = a * b concurrently. k=0; // i=4; // j=4; for (i = 0; i < 18; i++) { for (j = 0; j < 18; j++) { info[k].row = i; info[k].col = j; if (pthread_create(&thread[k], NULL, &compute_C_ij, (void *)&info[k]) != 0) { printf("Error creating thread %d\n", k); } pthread_join(thread[k], NULL); k++; } // k++; } //Full results //printf("The Result of matrix multiplication a * b is: \n"); //for(i=0; i<18; i++){ // for(j=0; j<18; j++){ // printf("%d\t", c[i][j]); // } // printf("\n"); //} //diagonal to check with manual provided values. printf("The Diagonal of matrix multiplication a * b is: \n"); for(i=0; i<18; i++){ printf("%d", c[i][i]); printf("\n"); } //for(i=0; i<10; i++){ //pthread_join(thread[k], NULL); //} pthread_exit(NULL); }
run
|
edit
|
history
|
help
0
NOT a String in C
Official Hello World
Welcome1
++i vs i++
bit shift and mask
Replacing Evens and Odd with ones and zeroes in a 3-Dimensional array
141122_PI6
Sunday assignment v2
Text to ASCII (top)
HelloWorld