2012-06-29 39 views
2

http://www.thesalmons.org/john/random123/releases/1.00/docs/index.htmlRandom123使用Visual Studio的

我有一個很難看的OpenCL和random123作爲即時通訊新的OpenCL的例子產生針對OpenCL隨機數和我即時通訊使用時,我不知道我該如何使用所提供的信息Visual Studio 2010中

任何人誰可以組成一個指南與上面的lib產生隨機數,並使用Visual Studio 2010中

UPDATE: 我解決了它的下面,現在不知道我該怎麼辦改變種子,我在每次運行時都會得到隨機數字。

int main(int argc, char **argv) 
{  
    const char *kernelname = "counthits"; 
    unsigned count =10000; 

    cl_int    err; 
    cl::Context   cl_context; 
    cl::Program   program; 
    cl::Kernel   cl_kernel; 
    cl::Buffer   cl_out; 
    cl::CommandQueue cl_queue; 

    size_t i, nthreads, hits_sz; 
    size_t cores, work_group_size; 
    cl_uint2 *   hits_host; 

    double    d = 0.; // timer 

    d = timer(&d); 
    progname = argv[0]; 

    std::vector<cl::Platform> platformList; 
    CHECK(cl::Platform::get(&platformList));   
    CHECKERR( cl_context = createCLContext(CL_DEVICE_TYPE_GPU,cl_vendor::VENDOR_AMD, &err)); 

    std::vector<cl::Device> devices; 
    CHECKERR(devices = cl_context.getInfo<CL_CONTEXT_DEVICES>(&err)); 


    size_t length = 0; 
    const char * sourceStr = loadFileToString("pi_opencl_kernel.ocl","",&length); 

    cl::Program::Sources sources(1, std::make_pair(sourceStr, length)); 
    program = cl::Program(cl_context, sources); 

    CHECK(program.build(devices,"-I D:\\libs\\Random123\\1.06\\include")); 

    CHECKERR(work_group_size = devices[0].getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>(&err)); 
    CHECKERR(cores = devices[0].getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>(&err)); 
    cores *= 16*4; //Tahiti. 

    if (work_group_size > 64) work_group_size /= 2; 
    nthreads = cores * work_group_size*32; //2048*128 = 262144 

    if (count == 0) 
    count = NTRIES/nthreads; //38 

    printf("Count: %lu\n",count); 



    hits_sz = nthreads * sizeof(hits_host[0]);//2097152 
    CHECKNOTZERO(hits_host = (cl_uint2 *)malloc(hits_sz)); 

    CHECKERR (cl_out = cl::Buffer( cl_context, CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, hits_sz, hits_host, &err)); 
    CHECKERR (cl_kernel = cl::Kernel(program,kernelname,&err)); 
    CHECK  (cl_kernel.setArg(0, count)); 
    CHECK  (cl_kernel.setArg(1, cl_out)); 

    CHECKERR (cl_queue = cl::CommandQueue(cl_context, devices[0], 0, &err)); 
    cl::Event event; 

    CHECK(cl_queue.enqueueNDRangeKernel(cl_kernel,cl::NullRange,cl::NDRange(nthreads), cl::NDRange(work_group_size), NULL, &event)); 
    event.wait(); 
    CHECK(cl_queue.enqueueReadBuffer(cl_out, CL_TRUE, 0,hits_sz, hits_host)); 

    unsigned long hits = 0, tries = 0; 
    for (i = 0; i < nthreads; i++) { 
#ifdef _DEBUG 
     printf("%lu %u %u\n", (unsigned long)i, hits_host[i].s[0], hits_host[i].s[1]); 
#endif 
    hits += hits_host[i].s[0]; 
    tries += hits_host[i].s[1]; 
    } 


    return pi_check(hits, tries); 
} 

內核:

#include <Random123/threefry.h> 

/* 
* counthits generates 2*n x,y points and returns hits[tid] with 
* the count of number of those points within the unit circle on 
* each thread. 
*/ 
__kernel void counthits(unsigned n, __global uint2 *hitsp) { 
    unsigned tid = get_global_id(0); 
    unsigned hits = 0, tries = 0; 
    threefry4x32_key_t k = {{tid, 0xdecafbad, 0xfacebead, 0x12345678}}; 
    threefry4x32_ctr_t c = {{0, 0xf00dcafe, 0xdeadbeef, 0xbeeff00d}}; 
    while (tries < n) { 
     union { 
      threefry4x32_ctr_t c; 
      int4 i; 
     } u; 
     c.v[0]++; 
     u.c = threefry4x32(c, k); 
     long x1 = u.i.x, y1 = u.i.y; 
     long x2 = u.i.z, y2 = u.i.w; 
     if ((x1*x1 + y1*y1) < (1L<<62)) { 
      hits++; 
     } 
     tries++; 
     if ((x2*x2 + y2*y2) < (1L<<62)) { 
      hits++; 
     } 
     tries++; 
    } 
    hitsp[tid].x = hits; 
    hitsp[tid].y = tries; 
} 
+0

[你嘗試過什麼?](http://mattgemmell.com/2008/12/08/what-have-you-tried/)我們在這裏不是「寫指南」,但回答有關問題單具體問題。 –

+1

你好 - 我actuly succeded昨天深夜:我的主要問題是如何建立與頭文件的內核與的lib給出的例子。解決方案是使用參數作爲一個使用普通的編譯器等的gcc是:program.build(設備,「 - ID:\\庫\\ Random123 \\ 1.06 \\包括」) –

+0

下一步,我需要找出如何我可以管理/設置種子,例如我每次運行都會得到新的隨機數字。任何想法? - 我將在一些問題中發佈代碼。 –

回答

0

我沒有測試過這一點,但大致說來,類似如下:

  1. 嘗試改變counthits的簽名:

_KERNEL空隙counthits(無符號N,__global UINT2 * HITSP,無符號種子)

  • 替換0xdecafbad與種子

  • 添加

  • 字符* seedstr = GETENV( 「COUNTHITS_SEED」);

    無符號種子= seedstr? atoi(seedstr):0xdecafbad;

    ...

    CHECK(cl_kernel.setArg(2,種子));

    主程序(這setArg來setArg(1,後...),你可以的)。