OpenCLを使用してn次元の点間のユークリッド距離を計算しています。n次元ポイントの2つのリストを取得し、最初のテーブルのすべてのポイントから2番目のテーブルのすべてのポイントまでの距離だけを含む配列を返す必要があります。
私のアプローチは、通常の二重ループを実行することです(Table1のすべてのポイント{Table2のすべてのポイント{...}}次に、並列のポイントのすべてのペアに対して計算を実行します。
次に、ユークリッド距離は3つの部分に分割されます。1。ポイントの各次元間の差を取ります2.その差を2乗します(まだすべての次元に対して)3。2で得られたすべての値を合計します。4。の平方根を取ります3.で取得した値(この例では、このステップは省略されています)。
すべての違いの合計を累積しようとするまで、すべてが魅力のように機能します(つまり、上記の手順のステップ3、以下のコードの49行目を実行します)。
テストデータとして、それぞれ2ポイントのDescriptorListsを使用しています。DescriptorList1:001,002,003、...、127,128; (p1)129,130,131、...、255,256; (p2)
DescriptorList2:000,001,002、...、126,127; (p1)128,129,130、...、254,255; (p2)
したがって、結果のベクトルの値は128、2064512、2130048、128になります。現在、実行ごとに変化する乱数を取得しています。
私が間違っていることについての助けやリードに感謝します。うまくいけば、私が取り組んでいるシナリオについてすべてが明確になっています。
#define BLOCK_SIZE 128
typedef struct
{
//How large each point is
int length;
//How many points in every list
int num_elements;
//Pointer to the elements of the descriptor (stored as a raw array)
__global float *elements;
} DescriptorList;
__kernel void CompareDescriptors_deb(__global float *C, DescriptorList A, DescriptorList B, int elements, __local float As[BLOCK_SIZE])
{
int gpidA = get_global_id(0);
int featA = get_local_id(0);
//temporary array to store the difference between each dimension of 2 points
float dif_acum[BLOCK_SIZE];
//counter to track the iterations of the inner loop
int loop = 0;
//loop over all descriptors in A
for (int i = 0; i < A.num_elements/BLOCK_SIZE; i++){
//take the i-th descriptor. Returns a DescriptorList with just the i-th
//descriptor in DescriptorList A
DescriptorList tmpA = GetDescriptor(A, i);
//copy the current descriptor to local memory.
//returns one element of the only descriptor in DescriptorList tmpA
//and index featA
As[featA] = GetElement(tmpA, 0, featA);
//wait for all the threads to finish copying before continuing
barrier(CLK_LOCAL_MEM_FENCE);
//loop over all the descriptors in B
for (int k = 0; k < B.num_elements/BLOCK_SIZE; k++){
//take the difference of both current points
dif_acum[featA] = As[featA]-B.elements[k*BLOCK_SIZE + featA];
//wait again
barrier(CLK_LOCAL_MEM_FENCE);
//square value of the difference in dif_acum and store in C
//which is where the results should be stored at the end.
C[loop] = 0;
C[loop] += dif_acum[featA]*dif_acum[featA];
loop += 1;
barrier(CLK_LOCAL_MEM_FENCE);
}
}
}