예제 #1
0
파일: hstore.c 프로젝트: chain710/beansdb
void hs_close(HStore *store)
{
    int i;
    if (!store) return;
    // stop optimizing
    store->op_start = store->op_end = 0;
    
    if (store->scan_threads > 1 && store->count > 1) {
        parallelize(store, bc_close);
    } else {
        for (i=0; i<store->count; i++){
            bc_close(store->bitcasks[i]);
        }
    }
    mgr_destroy(store->mgr);
    free(store);
}
예제 #2
0
void test_blocking_threads(TestContext *tc)
{
    Maze maze;
    Blocking_Helper bh;
    // Task Related Values 
    int thread_per_task;
    thread_per_task = 1;
   
    //Init Methods
    maze_build_from_file(&maze,"test.map");
    blocking_helper_init(&bh);
    blocking_helper_set_maze(&bh,&maze);
    
    // Create Tasks
    Task tasks[2];
    bzero(tasks, sizeof(Task)*2 );
    
    test_task_init(&tasks[0],(Proc)&st_client_wait_for_event,1,&bh,tc,NULL,NULL,NULL,NULL);
    test_task_init(&tasks[1],(Proc)&st_client_signal_update,1,&bh,tc,NULL,NULL,NULL,NULL);
    parallelize(tasks,2,thread_per_task);
    
}
예제 #3
0
파일: hstore.c 프로젝트: chain710/beansdb
HStore* hs_open(char *path, int height, time_t before, int scan_threads)
{
    if (NULL == path) return NULL;
    if (height < 0 || height > 3) {
        fprintf(stderr, "invalid db height: %d\n", height);
        return NULL; 
    }
    if (before != 0){
        if (before<0) {
            fprintf(stderr, "invalid time:%ld\n", before);
            return NULL;
        }else{
            fprintf(stderr, "serve data modified before %s\n", ctime(&before));
        }
    }
    
    char *paths[20], *rpath = path;
    int npath = 0;
    while ((paths[npath] = strsep(&rpath, ",:;")) != NULL) {
        if (npath >= MAX_PATHS) return NULL; 
        path = paths[npath];
        if (0 != access(path, F_OK) && 0 != mkdir(path, 0755)){
            fprintf(stderr, "mkdir %s failed\n", path);
            return NULL;
        }
        if (height > 1){
            // try to mkdir
            HStore *s = hs_open(path, height - 1, 0, 0);
            if (s == NULL){
                return NULL;
            }
            hs_close(s);
        }
        
        npath ++;
    }

    int i, j, count = 1 << (height * 4);
    HStore *store = (HStore*) malloc(sizeof(HStore) + sizeof(Bitcask*) * count);
    if (!store) return NULL;
    memset(store, 0, sizeof(HStore) + sizeof(Bitcask*) * count);
    store->height = height;
    store->count = count;
    store->before = before;
    store->scan_threads = scan_threads;
    store->op_start = 0;
    store->op_end = 0;
    store->op_limit = 0;
    store->mgr = mgr_create((const char**)paths, npath);
    if (store->mgr == NULL) {
        free(store);
        return NULL;
    }
    for (i=0; i<NUM_OF_MUTEX; i++) {
        pthread_mutex_init(&store->locks[i], NULL);
    }

    char *buf[20] = {0};
    for (i=0;i<npath;i++) {
        buf[i] = malloc(255);
    }
    for (i=0; i<count; i++){
        for (j=0; j<npath; j++) {
            path = paths[j];
            switch(height){
                case 0: sprintf(buf[j], "%s", path); break;
                case 1: sprintf(buf[j], "%s/%x", path, i); break;
                case 2: sprintf(buf[j], "%s/%x/%x", path, i>>4, i & 0xf); break;
                case 3: sprintf(buf[j], "%s/%x/%x/%x", path, i>>8, (i>>4)&0xf, i&0xf); break;
            }
        }
        Mgr *mgr = mgr_create((const char**)buf, npath);
        if (mgr == NULL) return NULL;
        store->bitcasks[i] = bc_open2(mgr, height, i, before);
    }
    for (i=0;i<npath;i++) {
        free(buf[i]);
    }
   
    if (store->scan_threads > 1 && count > 1) {
        parallelize(store, bc_scan);
    }else{
        for (i=0; i<count; i++) {
            bc_scan(store->bitcasks[i]);
        }
    }

    return store;
}
예제 #4
0
// Main function 
// *********************************************************************
int main(int argc, char **argv)
{
    // set and log Global and Local work size dimensions
    szLocalWorkSize = 16;
    szGlobalWorkSize = (iNumElements + szLocalWorkSize - 1) / szLocalWorkSize * szLocalWorkSize;  // rounded up to the nearest multiple of the LocalWorkSize

    // Allocate and initialize host arrays 
    std::vector<float> radius;
    std::vector<float4> position;
    std::vector<float4> emission;
    std::vector<float4> color;
    std::vector<cl_short> reflType;
    std::vector<float4> output;
    cl_int numSpheres = parallelize(spheres, radius, position, emission, color, reflType);

    output.resize(szGlobalWorkSize);

    // Create the OpenCL context on a GPU device
    cl_int err = 0;
    cxGPUContext = clCreateContextFromType(0, CL_DEVICE_TYPE_GPU, NULL, NULL, &err);
    CL_VERIFY(err);

    // Get the list of GPU devices associated with context
    clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, 0, NULL, &szParmDataBytes);
    cdDevices = (cl_device_id*)malloc(szParmDataBytes);
    clGetContextInfo(cxGPUContext, CL_CONTEXT_DEVICES, szParmDataBytes, cdDevices, NULL);

    cqCommandQue = clCreateCommandQueue(cxGPUContext, cdDevices[0], 0, 0);

    // Allocate the OpenCL buffer memory objects for source and result on the device GMEM
    cl_mem radiusBuf = createBuffer(cxGPUContext, radius);
    cl_mem positionBuf = createBuffer(cxGPUContext, position);
    cl_mem emissionBuf = createBuffer(cxGPUContext, emission);
    cl_mem colorBuf = createBuffer(cxGPUContext, color);
    cl_mem reflTypeBuf = createBuffer(cxGPUContext, reflType);
    cl_mem outputBuf = clCreateBuffer(cxGPUContext, CL_MEM_WRITE_ONLY, sizeof(output[0]) * output.size(), NULL, 0);
    
    std::string source = readFile(cSourceFile);
    const char *cSource = source.c_str();

    // Create the program
    cpProgram = clCreateProgramWithSource(cxGPUContext, 1, &cSource, 0, 0);

    // Build the program
    err = clBuildProgram(cpProgram, 0, NULL, NULL, NULL, NULL);
    CL_VERIFY(err);

    // Create the kernel
    ckKernel = clCreateKernel(cpProgram, "radiance", &err);
    CL_VERIFY(err);

    // Set the Argument values
    CL_VERIFY(clSetKernelArg(ckKernel, 0, sizeof(cl_mem), (void*)&radiusBuf));
    CL_VERIFY(clSetKernelArg(ckKernel, 1, sizeof(cl_mem), (void*)&positionBuf));
    CL_VERIFY(clSetKernelArg(ckKernel, 2, sizeof(cl_mem), (void*)&emissionBuf));
    CL_VERIFY(clSetKernelArg(ckKernel, 3, sizeof(cl_mem), (void*)&colorBuf));
    CL_VERIFY(clSetKernelArg(ckKernel, 4, sizeof(cl_mem), (void*)&reflTypeBuf));
    CL_VERIFY(clSetKernelArg(ckKernel, 5, sizeof(cl_int), (void*)&numSpheres));
    CL_VERIFY(clSetKernelArg(ckKernel, 6, sizeof(cl_mem), (void*)&outputBuf));
    CL_VERIFY(clSetKernelArg(ckKernel, 7, sizeof(cl_int), (void*)&width));
    CL_VERIFY(clSetKernelArg(ckKernel, 8, sizeof(cl_int), (void*)&height));

    // --------------------------------------------------------
    // Start Core sequence... copy input data to GPU, compute, copy results back

    // Launch kernel
    CL_VERIFY(clEnqueueNDRangeKernel(cqCommandQue, ckKernel, 1, NULL, &szGlobalWorkSize, &szLocalWorkSize, 0, NULL, NULL));

    // Synchronous/blocking read of results, and check accumulated errors
    CL_VERIFY(clEnqueueReadBuffer(cqCommandQue, outputBuf, CL_TRUE, 0, sizeof(output[0]) * output.size(), &output.front(), 0, NULL, NULL));

    writePPM(output);
    writeRaw(output);

    // Cleanup and leave
    Cleanup (EXIT_SUCCESS);
}