Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Nearly working
#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[1][1], b[1][1], c[1][1]); //debugging pthread_exit(NULL); } int main(int argc, char *argv[]) { //variables to loops. int i,j,k; k = 0; for(k=0; k<325; k++){ pthread_t thread[k]; //Thread variables thinfo info[k]; //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=1; // j=1; 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.%d\n", i, j); 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"); } pthread_exit(NULL); }
run
|
edit
|
history
|
help
0
Lab 9 v0.95
swapping using call by reference
Avance 2 de pregunta 5
hello kous
Simple Calculator v1.0
a3
table of 123
Ascii contrasena segura
strcpy template
fork