title: Parallelize the initialization of an array
body: <p>The code bellow is written in C for Linux. I was able to parallelize the computation of the array sum.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;pthread.h&gt;
  
  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 &lt; array_size; i++) {
          array[i] = 5;
      }
  
      gettimeofday(&amp;start,NULL);
  
      for(i=0; i &lt; num_threads; i++) {
          idx[i] = i;
          midsum[i * midsum_padding] = 0;
          pthread_create(&amp;tid[i], NULL, sum_thread, &amp;idx[i]);
      }
  
      for(i=0; i &lt; num_threads; i++) {
          pthread_join(tid[i], NULL);
          final_sum += midsum[i * midsum_padding];
      }
      gettimeofday(&amp;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 &lt; (idx + 1) * step; i++) {
          midsum[idx * midsum_padding] += array[i];
      }
  }
  </code></pre>
  
  <p>But how can I change the array initialization code so that it is also parallelized?  That is, each thread should do some of the array initialization, too.</p>