下面的代码是用C语言编写的。我能够并行计算数组和。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
int array_size;
int num_threads;
int step;
int midsum_padding;
int* array;
int* midsum;
void* sum_thread (void * arg);
int main (int argc, char** argv) {
if(argc != 4) {
printf("Need 3 arguments\n");
return 0;
}
array_size = atoi(argv[1]);
num_threads = atoi(argv[2]);
midsum_padding = atoi(argv[3]);
int i = 0;
int final_sum = 0;
struct timeval start, end;
double elapsed;
pthread_t *tid = malloc(num_threads*sizeof(pthread_t));
int *idx = malloc(num_threads*sizeof(int));
array = malloc(array_size*sizeof(int));
midsum = malloc(num_threads*midsum_padding*sizeof(int));
step = array_size/num_threads;
for(i=0; i < array_size; i++) {
array[i] = 5;
}
gettimeofday(&start,NULL);
for(i=0; i < num_threads; i++) {
idx[i] = i;
midsum[i * midsum_padding] = 0;
pthread_create(&tid[i], NULL, sum_thread, &idx[i]);
}
for(i=0; i < num_threads; i++) {
pthread_join(tid[i], NULL);
final_sum += midsum[i * midsum_padding];
}
gettimeofday(&end, NULL);
elapsed = ((double)end.tv_sec + 0.000001*(double)end.tv_usec)-
((double)start.tv_sec + 0.000001*(double)start.tv_usec);
printf("%lf\n", elapsed);
return 0;
}
void *sum_thread (void* arg) {
int *val = arg;
int idx = *val;
int i = 0;
for (i = idx * step; i < (idx + 1) * step; i++) {
midsum[idx * midsum_padding] += array[i];
}
}
但是如何更改数组初始化代码以使其也可以并行化?也就是说,每个线程也应该进行一些数组初始化。