3

それで、他の投稿で私はC時間測定について質問しました。ここで、Cの「関数」とOpenCLの「関数」の結果を比較する方法を知りたいです。

これはホストOpenCLとCのコードです

#define PROGRAM_FILE "sum.cl"
#define KERNEL_FUNC "float_sum"
#define ARRAY_SIZE 1000000


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include <CL/cl.h>

int main()
{
    /* OpenCL Data structures */

    cl_platform_id platform;
    cl_device_id device;
    cl_context context;
    cl_program program;
    cl_kernel kernel;    
    cl_command_queue queue;
    cl_mem vec_buffer, result_buffer;

    cl_event prof_event;;

    /* ********************* */

    /* C Data Structures / Data types */
    FILE *program_handle; //Kernel file handle
    char *program_buffer; //Kernel buffer

    float *vec, *non_parallel;
    float result[ARRAY_SIZE];

    size_t program_size; //Kernel file size

    cl_ulong time_start, time_end, total_time;

    int i;
    /* ****************************** */

    /* Errors */
    cl_int err;
    /* ****** */

    non_parallel = (float*)malloc(ARRAY_SIZE * sizeof(float));
    vec          = (float*)malloc(ARRAY_SIZE * sizeof(float));

    //Initialize the vector of floats
    for(i = 0; i < ARRAY_SIZE; i++)
    vec[i] = i + 1;

    /************************* C Function **************************************/
    clock_t start, end;

    start = clock();

    for( i = 0; i < ARRAY_SIZE; i++) 
    {
    non_parallel[i] = vec[i] * vec[i];
    }
    end = clock();
    printf( "Number of seconds: %f\n", (clock()-start)/(double)CLOCKS_PER_SEC );

    free(non_parallel);
    /***************************************************************************/




    clGetPlatformIDs(1, &platform, NULL);//Just want NVIDIA platform
    clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);
    context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);

    // Context error?
    if(err)
    {
    perror("Cannot create context");
    return 1;
    }

    //Read the kernel file
    program_handle = fopen(PROGRAM_FILE,"r");
    fseek(program_handle, 0, SEEK_END);
    program_size = ftell(program_handle);
    rewind(program_handle);

    program_buffer = (char*)malloc(program_size + 1);
    program_buffer[program_size] = '\0';
    fread(program_buffer, sizeof(char), program_size, program_handle);
    fclose(program_handle);

    //Create the program
    program = clCreateProgramWithSource(context, 1, (const char**)&program_buffer, 
                    &program_size, &err);

    if(err)
    {
    perror("Cannot create program");
    return 1;
    }

    free(program_buffer);

    clBuildProgram(program, 0, NULL, NULL, NULL, NULL);

    kernel = clCreateKernel(program, KERNEL_FUNC, &err);

    if(err)
    {
    perror("Cannot create kernel");
    return 1;
    }

    queue = clCreateCommandQueue(context, device, CL_QUEU_PROFILING_ENABLE, &err);

    if(err)
    {
    perror("Cannot create command queue");
    return 1;
    }

    vec_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR,
                sizeof(float) * ARRAY_SIZE, vec, &err);
    result_buffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float)*ARRAY_SIZE, NULL, &err);

    if(err)
    {
    perror("Cannot create the vector buffer");
    return 1;
    }

    clSetKernelArg(kernel, 0, sizeof(cl_mem), &vec_buffer);
    clSetKernelArg(kernel, 1, sizeof(cl_mem), &result_buffer);

    size_t global_size = ARRAY_SIZE;
    size_t local_size = 0;

    clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, NULL, 0, NULL, &prof_event);

    clEnqueueReadBuffer(queue, result_buffer, CL_TRUE, 0, sizeof(float)*ARRAY_SIZE, &result, 0, NULL, NULL);
    clFinish(queue);



     clGetEventProfilingInfo(prof_event, CL_PROFILING_COMMAND_START,
           sizeof(time_start), &time_start, NULL);
     clGetEventProfilingInfo(prof_event, CL_PROFILING_COMMAND_END,
           sizeof(time_end), &time_end, NULL);
     total_time += time_end - time_start;

    printf("\nAverage time in nanoseconds = %lu\n", total_time/ARRAY_SIZE);



    clReleaseMemObject(vec_buffer);
    clReleaseMemObject(result_buffer);
    clReleaseKernel(kernel);
    clReleaseCommandQueue(queue);
    clReleaseProgram(program);
    clReleaseContext(context);

    free(vec);

    return 0;
}

そしてカーネルは:

__kernel void float_sum(__global float* vec,__global float* result){
    int gid = get_global_id(0);
    result[gid] = vec[gid] * vec[gid];
}

現在、結果は次のとおりです。

秒数:0.010000<-これはCコード用です

ナノ秒単位の平均時間=140737284<-OpenCL関数

0,1407秒はOpenCL時間カーネル実行の時間であり、C関数よりも長いです、それは正しいですか?OpenCLはC非並列アルゴリズムよりも高速であるはずだと思うので...

4

3 に答える 3

3

GPU での並列コードの実行は、CPU での実行より必ずしも高速ではありません。計算に加えて、GPU メモリとの間でデータを転送する必要があることも考慮してください。

あなたの例では、2 * N アイテムを転送し、O(N) 操作を並行して実行していますが、これは GPU の非常に非効率的な使用です。したがって、この特定の計算では CPU の方が実際に高速である可能性が非常に高くなります。

于 2012-04-14T17:14:56.247 に答える
2

助けを求めて彼女に来る他の人のために:OpenCLを使用したカーネルランタイムのプロファイリングの簡単な紹介

プロファイリングモードを有効にします。

cmdQueue = clCreateCommandQueue(context, *devices, CL_QUEUE_PROFILING_ENABLE, &err);

プロファイリングカーネル:

cl_event prof_event; 
clEnqueueNDRangeKernel(cmdQueue, kernel, 1 , 0, globalWorkSize, NULL, 0, NULL, &prof_event);

次のプロファイリングデータを読み取ります。

cl_ulong ev_start_time=(cl_ulong)0;     
cl_ulong ev_end_time=(cl_ulong)0;   

clFinish(cmdQueue);
err = clWaitForEvents(1, &prof_event);
err |= clGetEventProfilingInfo(prof_event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &ev_start_time, NULL);
err |= clGetEventProfilingInfo(prof_event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &ev_end_time, NULL);

カーネル実行時間を計算します。

float run_time_gpu = (float)(ev_end_time - ev_start_time)/1000; // in usec

あなたのアプローチ

total_time/ARRAY_SIZE

あなたが望むものではありません。作業項目ごとの実行時間が表示されます。

ナノ秒単位の操作/時間は、GFLOPS(1秒あたりのギガ浮動小数点演算)を提供します。

于 2012-10-25T15:33:47.973 に答える
2

これは、アプリケーションの大きな問題の 1 つです。

size_t global_size = ARRAY_SIZE;
size_t local_size = 0;

単一アイテムのワーク グループを作成しているため、ほとんどの GPU がアイドル状態になります。多くの場合、単一アイテムのワーク グループを使用すると、GPU の 1/15 しか使用されません。

代わりにこれを試してください:

size_t global_size = ARRAY_SIZE / 250; //important: local_size*global_size must equal ARRAY_SIZE
size_t local_size = 250; //a power of 2 works well. 250 is close enough, yes divisible by your 1M array size

ここで、グラフィック ハードウェアの ALU をより適切に飽和させる大きなグループを作成しています。カーネルは現在の方法で問題なく動作しますが、カーネル部分をさらに活用する方法もあります。

カーネルの最適化: ARRAY_SIZE を追加のパラメーターとしてカーネルに渡し、より最適なグループ サイズの少数のグループを使用します。また、local_size*global_size を ARRAY_SIZE と正確に等しくする必要もなくなります。ワークアイテムのグローバル ID は、このカーネルでは使用されず、合計サイズが渡されたため必要ありません。

__kernel void float_sum(__global float* vec,__global float* result,__global int count){
  int lId = get_local_id(0);
  int lSize = get_local_size(0);
  int grId = get_group_id(0);
  int totalOps = count/get_num_groups(0);
  int startIndex = grId * totalOps;
  int maxIndex = startIndex+totalOps;
  if(grId == get_num_groups(0)-1){
    endIndex = count;
  }
  for(int i=startIndex+lId;i<endIndex;i+=lSize){
    result[i] = vec[i] * vec[i];
  }
}

このような単純なカーネルには非常に多くの変数があると考えているかもしれません。カーネルを実行するたびに、データに対して 1 つだけではなく複数の操作が行われることに注意してください。以下の値を使用すると、私の radeon 5870 (20 計算ユニット) で、各作業項目は for ループで 781 または 782 の値を計算することになります。各グループは 50000 個のデータを計算します。私が使用する変数のオーバーヘッドは、4000 個のワーク グループを作成するオーバーヘッド (100 万個) よりもはるかに少ないです。

size_t global_size = ARRAY_SIZE / numComputeUnits;
size_t local_size = 64; //also try other multiples of 16 or 64 for gpu; or multiples of your core-count for a cpu kernel

numComputeUnits の値を取得する方法については、こちらを参照してください

于 2012-10-26T03:01:12.883 に答える