Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenMP和C的并行合并排序_C_Parallel Processing_Openmp - Fatal编程技术网

OpenMP和C的并行合并排序

OpenMP和C的并行合并排序,c,parallel-processing,openmp,C,Parallel Processing,Openmp,在我正在做的一门课程中,我正在尝试使用C语言中的openmp进行并行合并排序。我对并行编程领域相当陌生,尤其是openmp 我试图构建的程序将文件读入数组,对其进行处理并生成一个浮点数组。我需要对这个数组进行排序 我想提到的是,我们有一个运行时上限,我们不需要通过它来通过分配 但是,似乎出现了一些问题,排序错误。下面还给出了1225个元素的排序示例。从输出来看,我似乎非常接近,但可能每个线程都将已排序的元素放在了错误的位置 我查阅了互联网和书籍,并获得了以下合并排序的实现: void merge

在我正在做的一门课程中,我正在尝试使用C语言中的openmp进行并行合并排序。我对并行编程领域相当陌生,尤其是openmp

我试图构建的程序将文件读入数组,对其进行处理并生成一个浮点数组。我需要对这个数组进行排序

我想提到的是,我们有一个运行时上限,我们不需要通过它来通过分配

但是,似乎出现了一些问题,排序错误。下面还给出了1225个元素的排序示例。从输出来看,我似乎非常接近,但可能每个线程都将已排序的元素放在了错误的位置

我查阅了互联网和书籍,并获得了以下合并排序的实现:

void merge(float array[], float temp[], int left_start, int left_end, int right_start, int right_end){

    int left = left_start;
    int right = right_start;
    int index = 0;

    //as long as we have elements in the array
    while(left <= left_end && right<= right_end){
        if(array[left] < array[right])
            temp[index++] = array[left++];
        else
            temp[index++] = array[right++];
    }

    while(left <= left_end)    //copy remaining elements of the first list
        temp[index++] = array[left++];
    
    while(right <= right_end)    //copy remaining elements of the second list
        temp[index++] = array[right++];

    for(left=left_start,right=0;left<=right_end;left++,right++) //write the elements in the right order to the original array
        array[left]=temp[right];

    }



void merge_sort_parallel(float array[], float temp[], size_t start, size_t end, unsigned short number_of_threads){

    if(start < end){

            size_t middle = (int) (start + end)/2;

            #pragma omp parallel sections num_threads(number_of_threads) shared(number_of_threads)
            {
                #pragma omp section 
                    merge_sort_parallel(array, temp, start, middle, number_of_threads); 
                #pragma omp section
                    merge_sort_parallel(array ,temp, middle+1, end,number_of_threads); 
            }
            
            merge(array,temp, start,middle,middle+1,end); //merge the two sorted sub-arrays 
    }   

}
我们不能假设输入的大小,线程数作为命令行参数给出

我很高兴知道如何解决这个问题,因为我几乎放弃了

多谢各位

1.255
1.639
1.639
2.873
2.873
3.327
3.327
4.022
4.022
4.705
4.705
5.838
5.838
6.548
6.548
6.601
6.601
6.796
6.797
6.797
6.868
6.868
6.929
4.024
5.549
5.555
6.008
6.929
4.024
5.549
5.555
6.008
7.437
7.437
7.953
7.953
8.405
8.405
8.445
8.445
8.448
8.448
8.556
8.556
8.776
8.776
8.805
8.805
8.986
8.986
9.357
9.357
9.560
9.560
9.570
9.570
9.589
9.589
9.631
9.631
9.704
9.704
9.988
9.988
10.055
10.055
10.112
10.112
10.176
10.176
10.367
10.367
10.754
10.754
10.803
10.803
10.875
10.875
10.890
10.890
10.943
10.943
11.113
11.113
11.291
11.291
11.448
11.448
11.636
11.734
11.734
11.760
11.760
11.798
11.798
11.962
11.962
12.071
12.076
12.349
12.349
12.419
12.419
12.973
12.973
12.989
12.989
13.232
13.232
13.330
13.330
13.341
13.341
13.349
13.349
13.636
13.636
13.733
13.733
13.758
13.758
13.767
13.767
13.856
13.856
13.868
13.868
13.946
13.946
14.103
14.103
14.237
14.237
14.241
14.241
14.670
14.670
14.681
14.681
14.774
14.774
14.947
14.947
15.211
15.211
15.232
10.649
10.945
11.142
11.224
11.640
13.062
7.953
8.056
8.448
8.556
8.643
8.776
8.817
8.906
9.213
9.593
9.722
9.932
9.988
10.506
2.484
2.851
3.734
5.605
5.814
5.880
6.114
6.187
6.795
6.796
7.342
7.907
8.052
8.168
9.159
9.395
9.579
9.639
9.661
9.809
9.831
9.870
9.957
10.144
10.245
10.421
9.494
10.216
10.431
10.744
10.795
10.801
10.943
10.975
11.141
15.232
10.649
10.945
11.142
11.224
11.640
13.062
7.953
8.056
8.448
8.556
8.643
8.776
8.817
8.906
9.213
9.593
9.722
9.932
9.988
10.506
2.484
2.851
3.734
5.605
5.814
5.880
6.114
6.187
6.795
6.796
7.342
7.907
8.052
8.168
9.159
9.395
9.579
9.639
9.661
9.809
9.831
9.870
9.957
10.144
10.245
10.421
9.494
10.216
10.431
10.744
10.795
10.801
10.943
10.975
11.141
15.390
15.390
15.577
15.577
15.675
15.675
15.952
15.952
16.013
16.013
16.810
14.773
14.873
15.101
16.155
16.454
16.590
8.205
8.438
14.093
16.675
16.810
14.773
14.873
15.101
16.155
16.454
16.590
8.205
8.438
14.093
16.675
16.906
16.906
17.319
17.319
17.445
17.445
17.585
17.600
17.600
18.201
14.269
15.286
15.550
5.162
5.631
5.888
5.936
6.605
7.810
7.891
8.425
8.435
8.624
8.744
8.878
9.188
9.707
9.870
10.095
10.359
10.421
10.634
10.776
10.849
10.861
10.890
10.906
10.964
11.075
11.133
11.266
11.455
11.772
11.954
11.995
12.211
12.267
12.408
12.710
12.983
13.007
13.181
13.457
13.540
13.855
14.076
14.305
14.480
14.926
15.072
15.169
15.379
15.668
15.889
15.953
16.041
16.363
16.435
16.813
16.827
17.011
17.046
17.387
17.761
17.869
17.883
17.925
18.184
7.401
7.850
7.883
8.095
8.167
8.450
8.686
8.716
8.776
8.852
9.184
9.290
9.444
9.698
9.756
10.177
10.506
10.556
11.105
11.831
11.897
12.302
13.254
13.446
13.467
13.513
13.661
14.019
14.392
14.712
14.911
14.945
15.568
15.582
15.734
15.820
15.882
15.906
16.234
16.299
16.445
16.453
16.549
16.691
16.747
16.791
16.834
16.837
16.917
17.222
17.430
17.529
17.792
17.840
17.882
17.921
17.923
18.289
18.302
18.344
8.797
9.044
11.571
12.792
12.918
13.335
13.693
13.831
6.348
7.919
8.657
8.920
9.884
10.612
10.955
11.494
13.218
13.402
13.895
14.008
3.910
4.065
4.258
5.174
5.467
6.687
6.744
6.999
7.464
7.808
8.385
8.645
8.800
8.917
9.479
9.782
10.175
10.285
10.541
10.731
11.204
11.526
11.746
12.024
12.047
12.129
12.192
12.512
12.936
13.000
13.039
13.157
13.262
13.295
13.493
13.748
13.790
13.796
13.854
14.010
14.086
14.141
14.266
14.721
14.771
14.873
14.926
14.962
14.965
15.105
15.203
15.209
15.335
15.363
15.463
15.556
15.561
15.608
15.686
16.021
16.041
16.273
16.414
16.451
16.453
16.453
16.468
16.562
16.615
16.782
16.953
16.957
17.059
17.102
17.121
17.160
5.207
6.346
6.429
6.514
9.219
9.498
9.574
10.289
10.467
11.548
15.555
15.576
15.671
16.420
17.001
17.221
17.322
17.691
17.778
17.880
17.914
17.943
17.988
18.209
18.411
18.451
18.491
18.530
18.575
18.575
18.595
14.961
16.459
17.021
17.176
17.872
18.777
18.873
18.938
19.039
19.042
19.150
19.194
19.273
4.136
4.741
5.063
5.378
5.740
6.419
6.466
6.697
6.879
7.619
8.285
8.518
9.159
9.840
10.038
10.076
10.218
10.588
11.302
12.037
12.115
13.004
13.044
13.390
13.428
13.432
13.557
14.172
15.034
15.864
15.932
16.431
16.645
16.758
16.770
16.814
18.406
4.125
4.759
5.206
5.248
6.274
6.372
6.797
7.766
8.753
8.986
9.570
9.647
10.890
11.018
11.113
11.165
11.199
11.415
11.843
11.891
12.031
12.042
12.190
12.313
12.419
12.456
12.521
12.761
13.161
13.209
13.733
13.758
13.808
13.946
14.282
14.309
14.450
14.557
14.748
15.545
15.719
15.727
15.745
16.014
16.197
16.240
12.050
14.572
15.346
15.488
16.388
16.414
16.735
17.156
17.187
17.649
17.711
17.951
17.951
19.005
19.250
19.276
19.284
19.310
19.311
19.318
19.354
19.379
19.450
19.487
19.535
19.554
19.586
19.627
19.791
19.837
19.846
19.846
19.890
20.023
20.056
20.091
20.091
20.281
20.313
20.388
20.532
20.655
20.656
20.785
20.851
20.854
20.922
20.937
20.937
20.938
20.947
20.976
21.227
21.230
21.239
21.295
21.377
21.377
21.592
21.759
21.799
22.097
22.137
22.602
22.805
23.026
23.197
23.234
23.266
23.332
23.369
6.159
7.784
9.621
10.019
10.029
10.756
11.094
11.745
12.278
12.359
13.701
13.727
14.203
14.306
11.763
12.455
12.642
12.718
12.920
14.540
14.828
14.891
14.937
16.347
16.356
16.519
16.958
17.121
17.338
17.523
18.247
18.836
19.042
19.468
20.760
21.170
23.571
23.599
23.599
15.131
15.133
15.140
15.161
15.178
15.232
15.286
15.477
15.520
15.533
15.550
15.560
15.568
15.623
15.663
15.675
15.745
15.803
15.837
15.945
15.963
16.014
16.073
16.081
16.082
16.174
16.240
12.050
14.572
15.346
15.488
16.351
16.376
16.378
16.413
16.435
16.454
4.128
4.258
5.861
6.445
6.856
7.737
8.383
9.069
9.211
9.875
11.045
11.089
11.190
11.721
11.953
12.017
13.506
14.293
14.371
14.513
14.773
14.955
15.000
15.208
16.326
16.464
16.494
16.530
16.590
16.642
16.658
16.675
16.698
16.810
16.881
16.970
16.981
17.022
17.035
17.156
17.187
17.215
17.222
6.391
6.979
7.010
7.546
7.914
9.568
10.426
10.470
10.858
11.148
11.977
13.435
13.959
13.986
14.843
14.847
14.905
15.216
15.268
15.404
15.542
15.565
15.616
15.918
16.167
16.570
16.585
17.136
17.141
17.269
17.269
17.319
17.391
17.412
17.460
17.528
17.585
17.629
17.695
17.763
17.773
17.790
17.838
17.876
17.951
17.965
18.013
18.063
18.157
18.163
18.183
18.184
18.201
18.204
18.258
18.335
18.406
18.487
18.512
18.581
18.584
18.699
18.701
18.771
18.821
18.985
19.273
19.368
19.502
19.546
19.622
19.871
19.897
20.460
20.591
20.706
20.727
20.837
20.855
20.883
20.947
11.761
11.789
12.410
13.152
13.205
13.344
13.503
13.693
13.831
14.067
14.273
14.306
14.506
14.540
5.128
5.325
5.563
6.941
7.106
7.554
7.746
8.268
8.934
9.376
9.466
9.496
9.650
9.960
10.438
10.549
10.973
11.131
11.525
11.896
12.022
13.296
13.568
14.096
14.120
14.343
14.552
14.598
14.652
14.675
14.748
14.950
15.124
15.141
15.147
15.260
15.388
15.695
15.733
15.976
16.030
16.081
16.187
16.420
4.061
4.518
4.805
4.942
4.967
5.208
5.736
5.736
5.857
5.983
6.308
6.699
6.744
7.796
8.051
8.191
8.202
8.247
8.426
8.976
9.166
9.503
9.741
10.094
10.259
10.792
11.194
11.274
11.274
11.338
11.343
11.387
11.408
11.410
11.746
12.215
12.483
12.619
12.775
12.779
12.918
12.936
12.948
13.039
13.157
13.253
13.261
13.274
13.295
4.965
7.284
9.859
10.189
11.081
11.803
13.197
13.742
13.790
13.842
13.895
14.064
14.178
14.260
14.359
14.618
14.962
10.260
11.130
12.504
13.178
13.490
14.853
15.042
15.042
15.044
15.193
15.212
15.313
15.543
15.675
15.782
15.912
15.971
15.984
16.061
16.202
16.262
16.344
16.357
16.599
16.719
16.770
16.829
16.883
16.957
16.986
17.027
17.028
17.079
17.156
17.160
17.168
17.209
17.230
17.232
17.250
17.291
17.468
17.544
17.651
17.733
17.777
17.951
6.795
8.753
8.986
9.399
10.394
10.626
12.419
13.209
12.456
12.741
13.161
13.733
13.808
13.146
13.876
13.950
15.505
16.185
16.240
16.450
16.701
16.919
17.097
17.194
17.629
17.678
17.719
17.951
17.998
18.090
18.269
18.325
18.344
18.530
18.574
18.711
18.857
19.093
19.176
19.299
19.374
19.419
19.581
19.682
19.691
19.857
19.969
20.143
20.281
8.137
9.628
12.972
13.073
14.627
14.736
15.727
16.936
17.068
17.222
17.550
18.192
18.589
18.651
20.335
20.341
20.464
20.502
20.583
20.812
20.984
21.232
21.377
21.386
21.906
21.951
22.449
22.537
22.658
22.694
22.775
23.131
23.193
23.369
23.377
23.450
23.602
23.985
24.086
24.694
24.694
7.596
7.969
9.013
9.764
10.132
10.929
11.089
11.184
11.219
11.527
12.512
12.686
13.138
13.335
13.452
13.808
13.880
14.008
14.381
14.427
14.859
15.062
15.145
15.553
15.614
16.396
16.551
17.273
17.558
17.876
18.480
18.595
18.775
18.828
19.531
19.963
22.569
24.706
24.728
24.919
25.119
26.642
number of distances is 1225