Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ele709 lab 3.4
#include <stdio.h> #include <math.h> #include <sys/mman.h> #include <pthread.h> #include <malloc.h> #include <stdlib.h> #define NTHREADS 3 #define SIZE 1000000 #define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0])) %how to pseudo this code? struct thread_info { double a[18][16]; //arrays double b[16][18]; // arrays char op; // The math iteration to be performed: +,-,*,/ int maxitr; // The number of iterations to be performed double c[18][18]; // The result: c = a <op> b double exec_time; // The execution time per iteration in nsec }; void *func(void *arg) { %going to have to pass some information here. Struct? int i, j, k, sum, col, row; double c[18][18]; for (i = 0; i < LEN(a); i++) { for (j = 0; j < LEN(a[0]); j++) { for (k = 0; k < 16; k++) { sum = sum + a[i][k]*b[k][j]; } c[i][j] = sum; sum = 0; } }} return(c); %or the pointer for C. } %%%%%%%%%%%%%%%%%%%%%% int main(int argc, char *argv[]) { pthread_t loads[NTHREADS]; int no_of_threads; int i,j; %%%%%%%%%%%%borrowed from lab 3.3 - reduced to matrixes to simplify the code. pthread_t thread[]; thread_info_t info[]; info[].maxitr = (int)maxitr; info[].op = '+'; if (pthread_create(&thread[], NULL, &func, &info[]) != 0) { printf("Error in creating thread 1\n"); exit(1); } pthread_join(thread[], NULL); printf("Exec time for thread[] = %lf sec\n", info[].exec_time); pthread_exit(NULL); %%%%%%%%%%start of pseudo code and borrowed code from load.c %%%%%%%%% no_of_threads = NTHREADS; double a[18][16]; //arrays double b[16][18]; // arrays %initializing for(i=0; i<18; i++){ for(j=0; j<16; j++){ a[i][j] = i+j; } } for(i=0; i<16; i++){ for(j=0; j<18; j++){ b[i][j] = i+(2*j); } } %this is from load.c Want to recycle the lines below to run the threads to solve for the matrix concurrently. printf("Created %d threads to simulate load on the system.\n", no_of_threads); for (i = 0; i < no_of_threads; i++) { if (pthread_create(&loads[i], NULL, &func, &i) != 0) { printf("Error creating thread %d\n", i); exit(-1); } else { printf("Created Thread %d\n", i); } } printf("\nRunning ...\n"); printf("Use Ctrl-C to stop.\n"); pthread_exit(NULL); 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"); } }
run
|
edit
|
history
|
help
0
C - printf precision modifiers
A_141211_Tartaglia
tom grey wolf
Revprint
ternar
SOLUCION EJERCICIO SPOTIFY
asasas
Programação em C, testes
dining phylosopher
Reverse.CPP