Question: Write a code in C++ that will perfrom matrix multiplication in parallel using pthreads. The number of threads that can be used is input by
Write a code in C++ that will perfrom matrix multiplication in parallel using pthreads. The number of threads that can be used is input by the user and cannot exceed the amount of elements in the resultant matrix. I am having an issue with developing a load balancing algorithm to equall distribute the multiplications amongst the pthreads. For example if the user inputs 3 processes the threads should perform 3 sets of 4 multiplications and 1 set of 3 multiplications to implement all 15 multiplications. I have most of the code written, please modify this code to evenly distribute the matrix multiplications amongst the threads.
Some code is not shown below, essentially the user has input the number of processes to use as n and matrix1 and matrix2 contain values that are waiting to be multiplied by the threads. I need help implementing some logic that distributes the number of threads.
#include
for(i = 0; i < matrix1Rows; ++i) ///initializes values to 0 for(j = 0; j < matrix2Columns; ++j) { matrix3[i][j]=0; } #define NUM_THREADS n //this is entered by the user in code above //long tid; pthread_t threads[NUM_THREADS]; int rc; int i; for( i = 0; i < NUM_THREADS; i++ ) { cout << "main() : creating thread, " << i << endl; rc = pthread_create(&threads[i], NULL, multi, (void *)&i); if (rc) { cout << "Error:unable to create thread," << rc << endl; exit(-1); } } pthread_exit(NULL);
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
