Dividing loop iterations among threads

后端 未结 8 542
广开言路
广开言路 2021-01-05 03:41

I recently wrote a small number-crunching program that basically loops over an N-dimensional grid and performs some calculation at each point.

for (int i1 =          


        
8条回答
  •  时光说笑
    2021-01-05 04:29

    I would do something like this:

    void HistogramThread(int i1, Action HandleResults)
    {
        int[] histogram = new int[HistogramSize];
    
        for (int i2 = 0; i2 < N; i2++)
           for (int i3 = 0; i3 < N; i3++)
              for (int i4 = 0; i4 < N; i4++)
                 histogram[bin_index(i1, i2, i3, i4)] += 1;
    
        HandleResults(histogram);
    }
    
    int[] CalculateHistogram()
    {
        int[] histogram = new int[HistogramSize];
    
        ThreadPool pool; // I don't know syntax off the top of my head
        for (int i1=0; i1

    This way you don't need to share any memory, until the end.

提交回复
热议问题