简体   繁体   中英

Understanding behaviour of global id in OpenCL

OpenCL newbie question

I'm trying to write a kernel to perform some task over rectangular regions of an image.

OpenCL

__kernel void GrayBlockSignalSeparation
(
    __global float * rhos
)
{
    const int neighbourhoodSize = 60;

    const int x = get_global_id(0);
    const int y = get_global_id(1);

    rhos[x - 1 + (y-1)*(neighbourhoodSize) ] = x;
}

C#

private static void GraySignalSeparation
(                         
    ref Image<Gray, float> Signal
)
{
    float[] rhos = new float[(int)(image.Width / 60) * (int)(image.Height / 60)];                
    CLCalc.Program.Variable rhosVar = new CLCalc.Program.Variable(rhos);

    CLCalc.Program.MemoryObject[] args = new CLCalc.Program.MemoryObject[] 
    {                     
        rhosVar
    };
    GraySignalSeparationKernel.Execute(args, new int[] { (int)(image.Width / 60), (int)(image.Height / 60) });

    rhosVar.ReadFromDeviceTo(rhos);

}

When I inspect the rho values in the debugger I find that some of them appear to have been missed and are set to zero.

[0] 1
[1] 2
[2] 3
[3] 4
[4] 5
[5] 6
[6] 7
[7] 8
[8] 9
[9] 10
[10]    11
[11]    12
[12]    13
[13]    14
[14]    15
[15]    16
[16]    17
[17]    18
[18]    19
[19]    20
[20]    21
[21]    22
[22]    23
[23]    24
[24]    25
[25]    26
[26]    27
[27]    28
[28]    29
[29]    30
[30]    31
[31]    0
[32]    0
[33]    0
[34]    0
[35]    0
[36]    0
[37]    0
[38]    0
[39]    0
[40]    0
[41]    0
[42]    0
[43]    0
[44]    0
[45]    0
[46]    0
[47]    0
[48]    0
[49]    0
[50]    0
[51]    0
[52]    0
[53]    0
[54]    0
[55]    0
[56]    0
[57]    0
[58]    0
[59]    0
[60]    1
[61]    2
[62]    3
[63]    4
[64]    5
[65]    6
[66]    7
[67]    8
[68]    9
[69]    10
[70]    11
[71]    12
[72]    13
[73]    14
[74]    15
[75]    16
[76]    17
[77]    18
[78]    19
[79]    20
[80]    21
[81]    22
[82]    23
[83]    24
[84]    25
[85]    26
[86]    27
[87]    28
[88]    29
[89]    30
[90]    31
[91]    0
[92]    0
[93]    0
[94]    0
[95]    0
[96]    0
[97]    0
[98]    0
[99]    0
[100]   0
[101]   0
[102]   0
[103]   0
[104]   0
[105]   0
[106]   0
[107]   0
[108]   0
[109]   0
[110]   0
[111]   0
[112]   0
[113]   0
[114]   0
[115]   0
[116]   0
[117]   0
[118]   0
[119]   0
[120]   1
[121]   2
[122]   3
[123]   4
[124]   5
[125]   6
[126]   7
[127]   8
[128]   9
[129]   10
[130]   11
[131]   12
[132]   13
[133]   14
[134]   15
[135]   16
[136]   17
[137]   18
[138]   19
[139]   20
[140]   21
[141]   22
[142]   23
[143]   24
[144]   25
[145]   26
[146]   27
[147]   28
[148]   29
[149]   30
[150]   31
[151]   0
[152]   0
[153]   0
[154]   0
[155]   0
[156]   0
[157]   0
[158]   0
[159]   0
[160]   0
[161]   0
[162]   0
[163]   0
[164]   0
[165]   0
[166]   0
[167]   0
[168]   0
[169]   0
[170]   0
[171]   0
[172]   0
[173]   0
[174]   0
[175]   0
[176]   0
[177]   0
[178]   0
[179]   0
[180]   1
[181]   2
[182]   3
[183]   4
[184]   5
[185]   6
[186]   7
[187]   8
[188]   9
[189]   10
[190]   11
[191]   12
[192]   13
[193]   14
[194]   15
[195]   16
[196]   17
[197]   18
[198]   19
[199]   20
[200]   21
[201]   22
[202]   23
[203]   24
[204]   25
[205]   26
[206]   27
[207]   28
[208]   29
[209]   30
[210]   31
[211]   0
[212]   0
[213]   0
[214]   0
[215]   0
[216]   0
[217]   0
[218]   0
[219]   0
[220]   0
[221]   0
[222]   0
[223]   0
[224]   0
[225]   0
[226]   0
[227]   0
[228]   0
[229]   0
[230]   0
[231]   0
[232]   0
[233]   0
[234]   0
[235]   0
[236]   0
[237]   0
[238]   0
[239]   0
[240]   1
[241]   2
[242]   3
[243]   4
[244]   5
[245]   6
[246]   7
[247]   8
[248]   9
[249]   10
[250]   11
[251]   12
[252]   13
[253]   14
[254]   15
[255]   16
[256]   17
[257]   18
[258]   19
[259]   20
[260]   21
[261]   22
[262]   23
[263]   24
[264]   25
[265]   26
[266]   27
[267]   28
[268]   29
[269]   30
[270]   31
[271]   0
[272]   0
[273]   0
[274]   0
[275]   0
[276]   0
[277]   0
[278]   0
[279]   0
[280]   0
[281]   0
[282]   0
[283]   0
[284]   0
[285]   0
[286]   0
[287]   0
[288]   0
[289]   0
[290]   0
[291]   0
[292]   0
[293]   0
[294]   0
[295]   0
[296]   0
[297]   0
[298]   0
[299]   0
[300]   1
[301]   2
[302]   3
[303]   4
[304]   5
[305]   6
[306]   7
[307]   8
[308]   9
[309]   10
[310]   11
[311]   12
[312]   13
[313]   14
[314]   15
[315]   16
[316]   17
[317]   18
[318]   19
[319]   20
[320]   21
[321]   22
[322]   23
[323]   24
[324]   25
[325]   26
[326]   27
[327]   28
[328]   29
[329]   30
[330]   31
[331]   0
[332]   0
[333]   0
[334]   0
[335]   0
[336]   0
[337]   0
[338]   0
[339]   0
[340]   0
[341]   0
[342]   0
[343]   0
[344]   0
[345]   0
[346]   0
[347]   0
[348]   0
[349]   0
[350]   0
[351]   0
[352]   0
[353]   0
[354]   0
[355]   0
[356]   0
[357]   0
[358]   0
[359]   0
[360]   1
[361]   2
[362]   3
[363]   4
[364]   5
[365]   6
[366]   7
[367]   8
[368]   9
[369]   10
[370]   11
[371]   12
[372]   13
[373]   14
[374]   15
[375]   16
[376]   17
[377]   18
[378]   19
[379]   20
[380]   21
[381]   22
[382]   23
[383]   24
[384]   25
[385]   26
[386]   27
[387]   28
[388]   29
[389]   30
[390]   31
[391]   0
[392]   0
[393]   0
[394]   0
[395]   0
[396]   0
[397]   0
[398]   0
[399]   0
[400]   0
[401]   0
[402]   0
[403]   0
[404]   0
[405]   0
[406]   0
[407]   0
[408]   0
[409]   0
[410]   0
[411]   0
[412]   0
[413]   0
[414]   0
[415]   0
[416]   0
[417]   0
[418]   0
[419]   0
[420]   1
[421]   2
[422]   3
[423]   4
[424]   5
[425]   6
[426]   7
[427]   8
[428]   9
[429]   10
[430]   11
[431]   12
[432]   13
[433]   14
[434]   15
[435]   16
[436]   17
[437]   18
[438]   19
[439]   20
[440]   21
[441]   22
[442]   23
[443]   24
[444]   25
[445]   26
[446]   27
[447]   28
[448]   29
[449]   30
[450]   31
[451]   0
[452]   0
[453]   0
[454]   0
[455]   0
[456]   0
[457]   0
[458]   0
[459]   0
[460]   0
[461]   0
[462]   0
[463]   0
[464]   0
[465]   0
[466]   0
[467]   0
[468]   0
[469]   0
[470]   0
[471]   0
[472]   0
[473]   0
[474]   0
[475]   0
[476]   0
[477]   0
[478]   0
[479]   0
[480]   1
[481]   2
[482]   3
[483]   4
[484]   5
[485]   6
[486]   7
[487]   8
[488]   9
[489]   10
[490]   11
[491]   12
[492]   13
[493]   14
[494]   15
[495]   16
[496]   17
[497]   18
[498]   19
[499]   20
[500]   21
[501]   22
[502]   23
[503]   24
[504]   25
[505]   26
[506]   27
[507]   28
[508]   29
[509]   30
[510]   31
[511]   0
[512]   0
[513]   0
[514]   0
[515]   0
[516]   0
[517]   0
[518]   0
[519]   0
[520]   0
[521]   0
[522]   0
[523]   0
[524]   0
[525]   0
[526]   0
[527]   0
[528]   0
[529]   0
[530]   0
[531]   0
[532]   0
[533]   0
[534]   0
[535]   0
[536]   0
[537]   0
[538]   0
[539]   0
[540]   1
[541]   2
[542]   3
[543]   4
[544]   5
[545]   6
[546]   7
[547]   8
[548]   9
[549]   10
[550]   11
[551]   12
[552]   13
[553]   14
[554]   15
[555]   16
[556]   17
[557]   18
[558]   19
[559]   20
[560]   21
[561]   22
[562]   23
[563]   24
[564]   25
[565]   26
[566]   27
[567]   28
[568]   29
[569]   30
[570]   31
[571]   0
[572]   0
[573]   0
[574]   0
[575]   0

Try this:

rhos[x + (y)*(correct_stride) ] = x;

where correct_stride = image.Width / 60;

The global id's are indexed from 0 to upwards. It forms a sequence like 0,1,2,3,4... and not 1,2,3,4... If we are strict you are invoking undefined behavior as your first index performs a write outside the memory you originally allocated.

Also

float[] rhos = new float[(int)(image.Width / 60) * (int)(image.Height / 60)];

Worries me a bit. Is your image exactly 3600x3600? Otherwise your stride is wrong. The neighbourhoodsize variable must be the width of the picture segment you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM