So I have a app that can spawn any given number of threads. So I like this code to become multithreaded
void *some_thread_fuction(void *param)
{
struct some_struct *obj=(struct some_struct *)param;
int m=obj->m;
int n=...
double t[m 2][n 2]={0};
for (i=0; i <= m 1; i ) {
for (j=0; j <= n 1; j ) {
t[i][j] = 30.0;
}
}
for (i=1; i <= m; i ) {
t[i][0] = 40.0;
t[i][n 1] = 90.0;
}
for (j=1; j <= n; j ) {
t[0][j] = 30.0;
t[m 1][j] = 50.0;
}
memcpy(global_t,t,...);
}
I am having simple reasoning issue as to why I like to make this a multi-threaded program. but it make sense because if I have 5 threads (assumed I am taking how many number of threads to spawn at program start in program parameter) and n=20 m=20 which is also fed at program start as parameters then I can try working on 0-4 in one thread, 5-8 in second thread and so on until 16-20 in last iteration of first loop(just an example, because m=etc n=etc and number of threads can be anything values fed by user).
But more importantly I am having tough time as to how to even dissect the three for
loops to distribute the processing over amount of work to multiple threads to completion of all the loops in this code. Can any one help me out on this. simple code, so its just a real world example that I am having tough understanding how to do it in code for a threaded program for this senario
CodePudding user response:
Move this piece of code into a function:
for (i=0; i <= m 1; i ) {
for (j=0; j <= n 1; j ) {
t[i][j] = 30.0;
}
}
as follows:
void initialize(double t[], int start_i, int end_i, int n) {
for (i=start_i; i <= end_i; i ) {
for (j=0; j <= n 1; j ) {
t[i][j] = 30.0;
}
}
}
You can then split the interval [0 m 1] into as 5 intervals and call the initialize function from each thread for each interval.
That said, there must be more efficient ways to achieve the same thing using some copy instructions.