/*! @brief Reads in the kernel source code and compiles it @detailed Reads in the kernel source code and compiles it. The output from the compiler can be printed to the terminal with the showCompileLog variable. Returns a cl_program which is used to enqueue all the opencl kernels. */ cl_program compileKernelsFromSource(const char* fileName, const char* opt, cl_context context, cl_device_id* deviceList, cl_uint numDevicesToUse, uint showCompileLog){ cl_int status; /*For error checking*/ FILE *fp; /*File containing kernel source code*/ /* size_t source_size; /\*Size*\/ */ char *source_str; /*c-style string containing source code*/ cl_program prog; /*The compiled kernel*/ char buildOptions[200]; /*Contains options to OpenCL kernel compiler*/ /* Only required if showCompileLog flag is > 0 */ char *buffer; /* Pointer to c-style string containing compiler output - for debugging */ size_t len; /* Size of c-style string */ #define MAX_SOURCE_SIZE (0x100000) /* Load kernel source code */ fp = fopen(fileName, "rb"); if (!fp) { syslog(LOG_ERR, "There was an error opening %s, program will exit", fileName); fprintf(stderr, "There was an error opening %s, program will exit", fileName); exit(-1); } source_str = (char *)malloc(MAX_SOURCE_SIZE); /* source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp); */ fread(source_str, 1, MAX_SOURCE_SIZE, fp); fclose(fp); prog = clCreateProgramWithSource(context, 1, (const char**)&source_str, NULL, &status); statusCheck(status, "clCreateProgramWithSource"); free(source_str); /* ... with build options to include files in the same directory */ /* sprintf(buildOptions,"-I../include -cl-denorms-are-zero -cl-finite-math-only"); */ sprintf(buildOptions,"-I../include %s", opt); status = clBuildProgram(prog, numDevicesToUse, deviceList, buildOptions, NULL, NULL); /* Vital for tracking down errors in the kernel source code.*/ if(showCompileLog > 0){ clGetProgramBuildInfo(prog, deviceList[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &len); buffer = (char*)malloc(len); clGetProgramBuildInfo(prog, deviceList[0], CL_PROGRAM_BUILD_LOG, len, buffer, NULL); if( status != CL_SUCCESS){ syslog(LOG_ERR, "%s\n", buffer); fprintf(stderr, "%s\n", buffer); } else{ syslog(LOG_INFO, "%s\n", buffer); printf("%s\n", buffer); } free(buffer); } statusCheck(status, "clBuildProgram"); return prog; }
int main() { srand(time(NULL)); int randNum = rand(); int status; int testNum = 1; int player = 1; struct gameState gs; int k[10] = {adventurer, smithy, ambassador, embargo, gardens, great_hall, tribute, steward, sea_hag, minion}; printf("~~~~~BEGIN TESTING discardCard() ~~~~~\n"); status = initializeGame(2, k, randNum, &gs); assert(status == 0); int initialDiscard = gs.discardCount[player]; int initialHand = gs.handCount[player]; /* Test 1: Discard one card; check hand */ discardCard(0, player, &gs, 0); status = gs.handCount[player] == initialHand - 1; statusCheck(status, testNum); testNum++; /* Test 2: Check discards */ status = gs.discardCount[player] == initialDiscard + 1; statusCheck(status, testNum); testNum++; /* Test 3: Discard three cards; check */ discardCard(0, player, &gs, 0); discardCard(0, player, &gs, 0); discardCard(0, player, &gs, 0); status = gs.discardCount[player] == initialDiscard + 4; statusCheck(status, testNum); testNum++; /* Test 4: Check hand */ status = gs.handCount[player] == initialHand - 4; statusCheck(status, testNum); testNum++; printf("~~~~~FINISH TESTING discardCard() ~~~~~\n"); return 0; }
// search only the capture moves, resolve all the capture moves to help mitigate the horizon effect. int SearchMv::quiescentSearch(int alpha, int beta, Board& board,s_searchParamas* params) { if ( (params->nodes & 2047) == 0) // go read input every 2048 node count. statusCheck(params); params->nodes++; int val = evaluate(board); if (val >= beta) return beta; if (val > alpha) alpha = val; MoveGen gen(&board, true); // caps moves only constructor for (vector<int>::iterator it = gen.caps_v.begin(), end = gen.caps_v.end(); it != end; ++it) { myMaker.makeMove(*it, board); val = -quiescentSearch(-beta, -alpha, board,params); myMaker.takeBack(board); if(params->stopped) return 0; if (val >= beta) return beta; if (val > alpha) alpha = val; } return alpha; }
int main() { srand(time(NULL)); int randNum = rand(); int status; int testNum = 1; int player = 1; struct gameState gs; int k[10] = {adventurer, smithy, ambassador, embargo, gardens, great_hall, tribute, steward, sea_hag, minion}; printf("~~~~~BEGIN TESTING village ~~~~~\n"); status = initializeGame(2, k, randNum, &gs); assert(status == 0); int initialHand = gs.handCount[player]; int initialDeck = gs.deckCount[player]; int initialActions = gs.numActions; villageEffect(0, player, &gs); /* Test 1: Check if one card was added to hand */ status = gs.handCount[player] == initialHand + 1; statusCheck(status, testNum); testNum++; /* Test 2: Check if one card was removed from deck */ status = gs.deckCount[player] == initialDeck - 1; statusCheck(status, testNum); testNum++; /* Test 3: Check if number of actions is increased by 2 */ status = gs.numActions == initialActions + 2; statusCheck(status, testNum); testNum++; printf("~~~~~FINISH TESTING village ~~~~~\n"); return 0; }
void moveDataBetweenCPUandGPU(cl_command_queue cq, buffer* theBuffer, void* array, uint directionFlag){ cl_int status; void* mappedArray = clEnqueueMapBuffer(cq, theBuffer->mem, CL_TRUE, CL_MAP_WRITE, 0, theBuffer->size, 0, NULL, NULL, &status); char errorDescription[1024]; sprintf(errorDescription, "clEnqueueMapBuffer (in moveDataBetweenCPUandGPU) %s", theBuffer->name); statusCheck(status, errorDescription); if(directionFlag == 0){ memcpy(mappedArray, array, theBuffer->size); } else{ memcpy(array, mappedArray, theBuffer->size); } status = clEnqueueUnmapMemObject(cq, theBuffer->mem, mappedArray, 0, NULL, NULL); sprintf(errorDescription, "clEnqueueUnmapMemObject (in moveDataBetweenCPUandGPU) %s", theBuffer->name); statusCheck(status, errorDescription); clFinish(cq); }
/*! @brief Non-blocking function to copy data from GPU. @Detailed Has to be non-blocking in order to allow asynchronous memory transfers */ cl_event readBuffer(cl_command_queue cq, buffer* theBuffer, void* array, cl_uint wlSize, const cl_event* wl){ // cl_event* copyEvent = malloc(sizeof(cl_event)); cl_event copyEvent; cl_int status = clEnqueueReadBuffer(cq, theBuffer->mem, CL_FALSE, 0, theBuffer->size, array, // wlSize, wl, copyEvent); wlSize, wl, ©Event); char errorDescription[1024]; sprintf(errorDescription, "clEnqueueReadBuffer (in readBuffer) %s", theBuffer->name); statusCheck(status, errorDescription); // return (*copyEvent); return copyEvent; }
/*! @brief Creates a cl_kernel and checks it for errors. @detailed Utilizes the optional error checking functionality provided by opencl, wrapped up nicely. */ cl_kernel createKernel(cl_program prog, const char* kernelName){ cl_int status; // for error checking // Call the openCL function to create a kernel cl_kernel newKernel = clCreateKernel(prog, kernelName, &status); // description of function and check error char errorDescription[1024]; sprintf(errorDescription, "createKernel %s", kernelName); statusCheck(status, errorDescription); return newKernel; }
int UserOnlineSettingChanged(WPARAM hContact, LPARAM lParam) { DBCONTACTWRITESETTING *cws=(DBCONTACTWRITESETTING*)lParam; char *szProto = GetContactProto(hContact); if(hContact == NULL || mir_strcmp(cws->szSetting,"Status")) return 0; if (szProto && (CallProtoService(szProto, PS_GETCAPS, PFLAGNUM_1, 0) & PF1_IM)) { int newStatus = cws->value.wVal; int oldStatus = db_get_w(hContact,"UserOnline","OldStatus",ID_STATUS_OFFLINE); if (newStatus != oldStatus && hContact != NULL && newStatus != ID_STATUS_OFFLINE) { DBVARIANT dbv; if (!db_get_ts(hContact, modname, "PounceMsg", &dbv) && (dbv.ptszVal[0] != '\0')) { // check my status if (statusCheck(db_get_w(hContact, modname, "SendIfMyStatusIsFLAG", 0), CallProtoService(szProto, PS_GETSTATUS,0,0)) // check the contacts status && statusCheck(db_get_w(hContact, modname, "SendIfTheirStatusIsFLAG", 0), newStatus)) { // check if we r giving up after x days if (CheckDate(hContact)) { if (db_get_w(hContact, modname, "ConfirmTimeout", 0)) { SendPounceDlgProcStruct *spdps = (SendPounceDlgProcStruct *)mir_alloc(sizeof(SendPounceDlgProcStruct)); TCHAR *message = mir_tstrdup(dbv.ptszVal); // will get free()ed in the send confirm window proc spdps->hContact = hContact; spdps->message = message; CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_CONFIRMSEND), 0, SendPounceDlgProc, (LPARAM)spdps); // set the confirmation window to send the msg when the timeout is done mir_free(message); } else SendPounce(dbv.ptszVal, hContact); } } db_free(&dbv); } } } return 0; }
void setKernelArg(cl_kernel kernel, uint arg, buffer* theBuffer, const char* kernelName){ cl_int status; char errorDescription[1024]; if(theBuffer->mem != NULL){ /* For global memory */ /* Wants the size of the cl_mem object, i.e. the size on the CPU, not the GPU */ /* I guess the cl_mem object contains that information somewhere */ status = clSetKernelArg(kernel, arg, sizeof(theBuffer->mem), &theBuffer->mem); } else{ /* For local memory */ /* In the case of making a local buffer (I have set cl_mem to NULL), */ /* we need the size of the buffer we want to create on the GPU in bytes. */ status = clSetKernelArg(kernel, arg, theBuffer->size, NULL); } /* Opencl error checking */ sprintf(errorDescription, "setKernelArg %d %s", arg, kernelName); statusCheck(status, errorDescription); }
buffer* createBuffer(cl_context context, cl_mem_flags memFlags, size_t size, const char* typeChar, const char* bufferName){ cl_int status; /* I am not using alloc host pointer method, since I prefer to copy data explicitly in code. (I think it makes it more obvious what is going on.) */ cl_mem newBuffer = clCreateBuffer(context, memFlags, size, NULL, &status); char errorDescription[1024]; sprintf(errorDescription, "createBuffer %s", bufferName); statusCheck(status, errorDescription); /* Put all this stuff into my wrapped buffer*/ buffer* wrappedBuffer = malloc(sizeof(buffer)); wrappedBuffer->mem = newBuffer; wrappedBuffer->size = size; wrappedBuffer->name = bufferName; wrappedBuffer->typeChar = typeChar; return wrappedBuffer; }
int main() { srand(time(NULL)); int randNum = rand(); int status; int testNum = 1; struct gameState gs; int k[10] = {adventurer, smithy, ambassador, embargo, gardens, great_hall, tribute, steward, sea_hag, minion}; printf("~~~~~BEGIN TESTING isGameOver() ~~~~~\n"); /* Test 1: Initializing the game */ initializeGame(2, k, randNum, &gs); status = isGameOver(&gs); statusCheck(!status, testNum); testNum++; /* Test 2: Province is 0 */ gs.supplyCount[province] = 0; status = isGameOver(&gs); statusCheck(status, testNum); testNum++; gs.supplyCount[province] = 8; /* Test 3: End of turn */ endTurn(&gs); status = isGameOver(&gs); statusCheck(!status, testNum); testNum++; /* Test 4: Player draws card */ drawCard(1, &gs); status = isGameOver(&gs); statusCheck(!status, testNum); testNum++; /* Test 5: Player buys adventurer card */ buyCard(adventurer, &gs); status = isGameOver(&gs); statusCheck(!status, testNum); testNum++; /* Test 6: One supply pile is at 0 */ gs.supplyCount[0] = 0; status = isGameOver(&gs); statusCheck(!status, testNum); testNum++; /* Test 7: Two supply piles are at 0 */ gs.supplyCount[1] = 0; status = isGameOver(&gs); statusCheck(!status, testNum); testNum++; /* Test 8: Three supply pile are at 0 */ gs.supplyCount[2] = 0; status = isGameOver(&gs); statusCheck(status, testNum); testNum++; printf("~~~~~FINISH TESTING isGameOver() ~~~~~\n"); return 0; }
int SearchMv::alphaBeta(int depth,int alpha, int beta, Board& board, int mate, vector<int>& pLine, bool allowNull,s_searchParamas* params) { if ( (params->nodes & 2047) == 0) // go read input every 2048 node count. statusCheck(params); params->nodes++; int val; bool foundPV = false; vector<int> line; if (depth <= 0) //return evaluate(board); return quiescentSearch(alpha, beta, board,params); // null move forward prunning, will test more later /* if ( (depth >= 3) && (allowNull) && (board.isInCheck(board.turn) == false) && (isZugzwangChance(board) == false) ) { myMaker.makeNullMove(board); val = -alphaBeta(depth - 1 - R, -beta, -beta + 1, board, mate - 1, line, false,params); myMaker.takeNullMove(board); if (val >= beta) return beta; } */ vector<int> myMoves = getMoves(board); int numMoves = myMoves.size(); if (board.isInCheck(board.turn)) { if (numMoves == 0) return -mate; else depth++; // in check extention }else { if (numMoves == 0) // draw return 0; } for (vector<int>::iterator it = myMoves.begin(), end = myMoves.end(); it != end; ++it) { myMaker.makeMove(*it, board); if (isRepetion(board) || (board.hm_clock >= 50) || isInsuffcientMaterial(board)) { myMaker.takeBack(board); return 0; } if (foundPV) { val = -alphaBeta(depth - 1, -alpha - 1, -alpha, board ,mate - 1, line, true,params); if ((val > alpha) && (val < beta)) // Check for failure. val = -alphaBeta(depth - 1,-beta,-alpha, board, mate - 1, line, true,params); }else val = -alphaBeta(depth - 1,-beta,-alpha, board, mate - 1, line, true,params); myMaker.takeBack(board); if(params->stopped) return 0; if (val >= beta) return beta; if (val > alpha) { alpha = val; foundPV = true; pLine = line; pLine.insert(pLine.begin(), *it); } } return alpha; }
void getPlatformAndDeviceInfo(cl_platform_id* platformIds, cl_uint maxPlatforms, cl_uint myPlatform, cl_device_type devType){ /* See how many platforms are available.*/ cl_uint numPlatforms; cl_int status = clGetPlatformIDs(maxPlatforms, platformIds, &numPlatforms); statusCheck(status, "clGetPlatformIds"); printf("%d platform(s) detected.\n", numPlatforms); /* Get the names of the platforms and display to user. */ char platNames[maxPlatforms][100]; uint plat = 0; for(plat = 0; plat < numPlatforms; plat++){ size_t nameBufferLength; status = clGetPlatformInfo(platformIds[plat], CL_PLATFORM_NAME, sizeof(platNames[plat]), platNames[plat], &nameBufferLength); statusCheck(status, "clGetPlatformInfo"); printf("\tPlatform %d: %s\n", plat, platNames[plat]); } /* see how many devices our chosen platform platform ...*/ const cl_uint maxDevices = 4; cl_device_id deviceIds[maxDevices]; cl_uint numDevices; clGetDeviceIDs(platformIds[myPlatform], devType, maxDevices, deviceIds, &numDevices); statusCheck(status, "clGetDeviceIDs"); /* Give up if we can't find any devices on chosen platform */ if (numDevices==0) { syslog(LOG_ERR, "Error! 0 devices found on platform %s!\n", platNames[myPlatform]); syslog(LOG_ERR, "This normally means I can't talk to the X server for some reason.\n"); syslog(LOG_ERR, "Exiting program.\n"); fprintf(stderr, "Error! 0 devices found on platform %s!\n", platNames[myPlatform]); fprintf(stderr, "This normally means I can't talk to the X server for some reason.\n"); fprintf(stderr, "Exiting program.\n"); exit(1); } /* Prints useful information about the GPU architecture to the screen.*/ size_t maxWorkGroupSize; char devNames[maxDevices][100]; uint dev=0; for(dev = 0; dev < numDevices; dev++){ size_t length; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_NAME, sizeof(devNames[dev]), devNames, &length); statusCheck(status, "clGetDeviceInfo"); cl_bool available; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_AVAILABLE, sizeof(available), &available, &length); statusCheck(status, "clGetDeviceInfo"); cl_uint maxComputeUnits; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(maxComputeUnits), &maxComputeUnits, &length); statusCheck(status, "clGetDeviceInfo"); cl_uint maxWorkItemDimension; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS, sizeof(maxWorkItemDimension), &maxWorkItemDimension, &length); statusCheck(status, "clGetDeviceInfo"); size_t maxWorkItemSizes[maxWorkItemDimension]; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_MAX_WORK_ITEM_SIZES, sizeof(maxWorkItemSizes), &maxWorkItemSizes, &length); statusCheck(status, "clGetDeviceInfo"); status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_MAX_WORK_GROUP_SIZE, sizeof(maxWorkGroupSize), &maxWorkGroupSize, &length); statusCheck(status, "clGetDeviceInfo"); cl_ulong maxMemAllocSize = 0; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(maxMemAllocSize), &maxMemAllocSize, &length); statusCheck(status, "clGetDeviceInfo"); cl_ulong deviceLocalMemSize = 0; status = clGetDeviceInfo(deviceIds[dev], CL_DEVICE_LOCAL_MEM_SIZE, sizeof(deviceLocalMemSize), &deviceLocalMemSize, &length); statusCheck(status, "clGetDeviceInfo"); printf("\t\tDevice: %d\n", dev); printf("\t\tName: %s\n", devNames[dev]); printf("\t\tAvailable: "); if(available){printf("Yes");} else{printf("No");} printf("\n\t\tNumber of Compute Units: %d\n", maxComputeUnits); printf("\t\tNumber of Work Item dimensions: %d\n", maxWorkItemDimension); printf("\t\tMax Work Items for each dimension: "); uint dim=0; for(dim = 0; dim < maxWorkItemDimension; dim++){ printf("%ld ", maxWorkItemSizes[dim]);} printf("\n\t\tMax Work Group size: %ld\n", maxWorkGroupSize); printf("\t\tGlobal memory size (bytes): %ld\n", maxMemAllocSize); printf("\t\tLocal memory size (bytes): %ld\n", deviceLocalMemSize); } }