// Main: Program Entry Point // Responsible for initializing GLFW, OpenGL, OpenCL and running the main loop int main(int argc, char **argv){ GLFWwindow* window; //double time = glfwGetTime(); double loopStart; // GLFW Initialization ---------------------------------------------------------------------- // Set GLFW Error Callback Function glfwSetErrorCallback(error_callback); // Initialize GLFW if (!glfwInit()) exit(EXIT_FAILURE); // Set OpenGL Version (Use 3.3) glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Create Window window = glfwCreateWindow(windowSize[0], windowSize[1], "AttractorCL", NULL, NULL); // Check if window creation failed if (!window){ glfwTerminate(); exit(EXIT_FAILURE); } // Set current OpenGL Context glfwMakeContextCurrent(window); glfwSwapInterval(1); printf("wglGetCurrentDC(): %u\nGLFW DC: %u\n", (unsigned int)wglGetCurrentDC(), (unsigned int)GetDC(GetForegroundWindow())); // Load OpenGL3.3 Extensions (MUST BE AFTER CONTEXT CREATION) if(ogl_LoadFunctions() == ogl_LOAD_FAILED){ glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_FAILURE); } // Set GLFW Functions glfwSetKeyCallback(window, key_callback); glfwSetFramebufferSizeCallback(window, resize); // ------------------------------------------------------------------------------------------ // Initialize OpenGL initGL(); // Initialize OpenCL initCL(); glUseProgram(programGL); glm::mat4 mtx = calcViewTransforms(); GLint uniMtx = glGetUniformLocation(programGL, "mtx"); glUniformMatrix4fv(uniMtx, 1, GL_FALSE, glm::value_ptr(mtx)); glUseProgram(0); runSim(); readBuffer(); runSim(); readBuffer(); // Run main loop, checking the glfwWindowShouldClose flag for termination while (!glfwWindowShouldClose(window)){ loopStart = glfwGetTime(); glfwPollEvents(); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); runSim(); glUseProgram(programGL); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, bufParticle_GL); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, sizeof(Particle), (void*)(sizeof(cl_uint)+3*sizeof(cl_float))); glBindBuffer(GL_ARRAY_BUFFER, bufParticle_GL); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Particle), (void*)(sizeof(cl_uint) + 2*sizeof(cl_float4) + 3*sizeof(cl_float))); glDrawArrays(GL_POINTS, 0, NUM_PARTICLES); glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glUseProgram(0); glFinish(); glfwSwapBuffers(window); //printf("%f\n",1/(glfwGetTime()-loopStart)); } // Free Resources and Exit Program ----- killCL(); killGL(); glfwDestroyWindow(window); glfwTerminate(); return 0; }
//------------------------------------------------------------------------------ int checkBackend(int backend, int levels) { printf("*** checking backend : %s\n", g_BackendNames[backend]); if (backend == kBackendCL) { #ifdef OPENSUBDIV_HAS_OPENCL if (initCL(&g_clContext, &g_clQueue) == false) { printf(" Cannot initialize OpenCL, skipping...\n"); return 0; } #else printf(" No OpenCL available, skipping...\n"); return 0; #endif } int total = 0; #define test_catmark_edgeonly #define test_catmark_edgecorner #define test_catmark_flap #define test_catmark_pyramid #define test_catmark_pyramid_creases0 #define test_catmark_pyramid_creases1 #define test_catmark_cube #define test_catmark_cube_creases0 #define test_catmark_cube_creases1 #define test_catmark_cube_corner0 #define test_catmark_cube_corner1 #define test_catmark_cube_corner2 #define test_catmark_cube_corner3 #define test_catmark_cube_corner4 #define test_catmark_dart_edgeonly #define test_catmark_dart_edgecorner #define test_catmark_tent #define test_catmark_tent_creases0 #define test_catmark_tent_creases1 #define test_catmark_square_hedit0 #define test_catmark_square_hedit1 #define test_catmark_square_hedit2 #define test_catmark_square_hedit3 #define test_loop_triangle_edgeonly #define test_loop_triangle_edgecorner #define test_loop_icosahedron #define test_loop_cube #define test_loop_cube_creases0 #define test_loop_cube_creases1 #define test_bilinear_cube #ifdef test_catmark_edgeonly #include "../shapes/catmark_edgeonly.h" total += checkMesh( "test_catmark_edgeonly", catmark_edgeonly, levels, kCatmark, backend ); #endif #ifdef test_catmark_edgecorner #include "../shapes/catmark_edgecorner.h" total += checkMesh( "test_catmark_edgeonly", catmark_edgecorner, levels, kCatmark, backend ); #endif #ifdef test_catmark_flap #include "../shapes/catmark_flap.h" total += checkMesh( "test_catmark_flap", catmark_flap, levels, kCatmark, backend ); #endif #ifdef test_catmark_pyramid #include "../shapes/catmark_pyramid.h" total += checkMesh( "test_catmark_pyramid", catmark_pyramid, levels, kCatmark, backend ); #endif #ifdef test_catmark_pyramid_creases0 #include "../shapes/catmark_pyramid_creases0.h" total += checkMesh( "test_catmark_pyramid_creases0", catmark_pyramid_creases0, levels, kCatmark, backend ); #endif #ifdef test_catmark_pyramid_creases1 #include "../shapes/catmark_pyramid_creases1.h" total += checkMesh( "test_catmark_pyramid_creases1", catmark_pyramid_creases1, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube #include "../shapes/catmark_cube.h" total += checkMesh( "test_catmark_cube", catmark_cube, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_creases0 #include "../shapes/catmark_cube_creases0.h" total += checkMesh( "test_catmark_cube_creases0", catmark_cube_creases0, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_creases1 #include "../shapes/catmark_cube_creases1.h" total += checkMesh( "test_catmark_cube_creases1", catmark_cube_creases1, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_corner0 #include "../shapes/catmark_cube_corner0.h" total += checkMesh( "test_catmark_cube_corner0", catmark_cube_corner0, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_corner1 #include "../shapes/catmark_cube_corner1.h" total += checkMesh( "test_catmark_cube_corner1", catmark_cube_corner1, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_corner2 #include "../shapes/catmark_cube_corner2.h" total += checkMesh( "test_catmark_cube_corner2", catmark_cube_corner2, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_corner3 #include "../shapes/catmark_cube_corner3.h" total += checkMesh( "test_catmark_cube_corner3", catmark_cube_corner3, levels, kCatmark, backend ); #endif #ifdef test_catmark_cube_corner4 #include "../shapes/catmark_cube_corner4.h" total += checkMesh( "test_catmark_cube_corner4", catmark_cube_corner4, levels, kCatmark, backend ); #endif #ifdef test_catmark_dart_edgecorner #include "../shapes/catmark_dart_edgecorner.h" total += checkMesh( "test_catmark_dart_edgecorner", catmark_dart_edgecorner, levels, kCatmark, backend ); #endif #ifdef test_catmark_dart_edgeonly #include "../shapes/catmark_dart_edgeonly.h" total += checkMesh( "test_catmark_dart_edgeonly", catmark_dart_edgeonly, levels, kCatmark, backend ); #endif #ifdef test_catmark_tent #include "../shapes/catmark_tent.h" total += checkMesh( "test_catmark_tent", catmark_tent, levels, kCatmark, backend ); #endif #ifdef test_catmark_tent_creases0 #include "../shapes/catmark_tent_creases0.h" total += checkMesh( "test_catmark_tent_creases0", catmark_tent_creases0, levels, kCatmark, backend ); #endif #ifdef test_catmark_tent_creases1 #include "../shapes/catmark_tent_creases1.h" total += checkMesh( "test_catmark_tent_creases1", catmark_tent_creases1, levels, kCatmark, backend ); #endif #ifdef test_catmark_square_hedit0 #include "../shapes/catmark_square_hedit0.h" total += checkMesh( "test_catmark_square_hedit0", catmark_square_hedit0, levels, kCatmark, backend ); #endif #ifdef test_catmark_square_hedit1 #include "../shapes/catmark_square_hedit1.h" total += checkMesh( "test_catmark_square_hedit1", catmark_square_hedit1, levels, kCatmark, backend ); #endif #ifdef test_catmark_square_hedit2 #include "../shapes/catmark_square_hedit2.h" total += checkMesh( "test_catmark_square_hedit2", catmark_square_hedit2, levels, kCatmark, backend ); #endif #ifdef test_catmark_square_hedit3 #include "../shapes/catmark_square_hedit3.h" total += checkMesh( "test_catmark_square_hedit3", catmark_square_hedit3, levels, kCatmark, backend ); #endif #ifdef test_loop_triangle_edgeonly #include "../shapes/loop_triangle_edgeonly.h" total += checkMesh( "test_loop_triangle_edgeonly", loop_triangle_edgeonly, levels, kLoop, backend ); #endif #ifdef test_loop_triangle_edgecorner #include "../shapes/loop_triangle_edgecorner.h" total += checkMesh( "test_loop_triangle_edgecorner", loop_triangle_edgecorner, levels, kLoop, backend ); #endif #ifdef test_loop_saddle_edgeonly #include "../shapes/loop_saddle_edgeonly.h" total += checkMesh( "test_loop_saddle_edgeonly", loop_saddle_edgeonly, levels, kLoop, backend ); #endif #ifdef test_loop_saddle_edgecorner #include "../shapes/loop_saddle_edgecorner.h" total += checkMesh( "test_loop_saddle_edgecorner", loop_saddle_edgecorner, levels, kLoop, backend ); #endif #ifdef test_loop_icosahedron #include "../shapes/loop_icosahedron.h" total += checkMesh( "test_loop_icosahedron", loop_icosahedron, levels, kLoop, backend ); #endif #ifdef test_loop_cube #include "../shapes/loop_cube.h" total += checkMesh( "test_loop_cube", loop_cube, levels, kLoop, backend ); #endif #ifdef test_loop_cube_creases0 #include "../shapes/loop_cube_creases0.h" total += checkMesh( "test_loop_cube_creases0", loop_cube_creases0,levels, kLoop, backend ); #endif #ifdef test_loop_cube_creases1 #include "../shapes/loop_cube_creases1.h" total += checkMesh( "test_loop_cube_creases1", loop_cube_creases1, levels, kLoop, backend ); #endif #ifdef test_bilinear_cube #include "../shapes/bilinear_cube.h" total += checkMesh( "test_bilinear_cube", bilinear_cube, levels, kBilinear, backend ); #endif if (backend == kBackendCL) { #ifdef OPENSUBDIV_HAS_OPENCL uninitCL(g_clContext, g_clQueue); #endif } return total; }
//------------------------------------------------------------------------------ static void #if GLFW_VERSION_MAJOR>=3 keyboard(GLFWwindow *, int key, int scancode, int event, int mods) { #else #define GLFW_KEY_ESCAPE GLFW_KEY_ESC keyboard(int key, int event) { #endif if (event == GLFW_RELEASE) return; if (g_hud.KeyDown(tolower(key))) return; switch (key) { case 'Q': g_running = 0; break; case 'F': fitFrame(); break; case '+': case '=': g_tessLevel++; break; case '-': g_tessLevel = std::max(g_tessLevelMin, g_tessLevel-1); break; case '.': g_moveModels = std::max(g_moveModels*2, 1); break; case ',': g_moveModels = std::max(g_moveModels/2, 0); break; case 'I': g_modelCount = std::max(g_modelCount/2, 1); rebuild(); break; case 'O': g_modelCount = std::min(g_modelCount*2, MAX_MODELS); rebuild(); break; case GLFW_KEY_ESCAPE: g_hud.SetVisible(!g_hud.IsVisible()); break; } } //------------------------------------------------------------------------------ static void callbackDisplayStyle(int b) { if (g_displayStyle == kVaryingColor or b == kVaryingColor or g_displayStyle == kFaceVaryingColor or b == kFaceVaryingColor) { // need to rebuild for varying reconstruct g_displayStyle = b; rebuild(); return; } g_displayStyle = b; } static void callbackKernel(int k) { g_kernel = k; #ifdef OPENSUBDIV_HAS_OPENCL if (g_kernel == kCL and g_clContext == NULL) { if (initCL(&g_clContext, &g_clQueue) == false) { printf("Error in initializing OpenCL\n"); exit(1); } } #endif #ifdef OPENSUBDIV_HAS_CUDA if (g_kernel == kCUDA and g_cudaInitialized == false) { g_cudaInitialized = true; cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() ); } #endif rebuild(); }
// Main Program //***************************************************************************** int main(int argc, char** argv) { pArgc = &argc; pArgv = argv; shrQAStart(argc, argv); // start logs cExecutableName = argv[0]; shrSetLogFileName ("oclPostProcessGL.txt"); shrLog("%s Starting...\n\n", argv[0]); // process command line arguments if (argc > 1) { bQATest = shrCheckCmdLineFlag(argc, (const char**)argv, "qatest"); bNoPrompt = shrCheckCmdLineFlag(argc, (const char**)argv, "noprompt"); } shrLog(" Image Width = %d, Image Height = %d, Blur Radius = %d\n\n", image_width, image_height, blur_radius); // init GL if(!bQATest) { InitGL(&argc, argv); // create pbo createPBO(&pbo_source); createPBO(&pbo_dest); // create texture for blitting onto the screen createTexture(&tex_screen, image_width, image_height); bGLinterop = shrTRUE; } // init CL if( initCL(argc, (const char**)argv) != 0 ) { return -1; } // init fps timer shrDeltaT (1); // Create buffers and textures, // and then start main GLUT rendering loop for processing and rendering, // or otherwise run No-GL Q/A test sequence shrLog("\n%s...\n", bQATest ? "No-GL test sequence" : "Standard GL Loop"); printf("\n" "\tControls\n" "\t(right click mouse button for Menu)\n" "\t[ ] : Toggle Post-Processing (blur filter) ON/OFF\n" "\t[ p ] : Toggle Processing (between GPU or CPU)\n" "\t[ a ] : Toggle OpenGL Animation (rotation) ON/OFF\n" "\t[+/=] : Increase Blur Radius\n" "\t[-/_] : Decrease Blur Radius\n" "\t[Esc] - Quit\n\n" ); if(!bQATest) { glutMainLoop(); } else { TestNoGL(); } Cleanup(EXIT_SUCCESS); }
int main(int argc, char** argv) { int args = 1; #ifdef USE_OPENCL if (argc < 10) { #else if (argc < 7) { #endif std::cout << "Not enough arguments" << std::endl; system("pause"); return 1; } DIM = util::toInt(argv[args++]); N = util::toInt(argv[args++]); K = util::toInt(argv[args++]); ITERATIONS = util::toInt(argv[args++]); RUNS = util::toInt(argv[args++]); #ifdef USE_OPENCL AM_LWS = util::toInt(argv[args++]); RP_LWS = util::toInt(argv[args++]); CT_LWS = util::toInt(argv[args++]); USE_ALL_DEVICES = util::toInt(argv[args++]); #else device_count = util::toInt(argv[args++]); #endif std::cout << "DIM = " << DIM << std::endl; std::cout << "N = " << N << std::endl; std::cout << "K = " << K << std::endl; std::cout << "ITERATIONS = " << ITERATIONS << std::endl; std::cout << "RUNS = " << RUNS << std::endl; #ifdef USE_OPENCL std::cout << "AM_LWS = " << AM_LWS << std::endl; std::cout << "RP_LWS = " << RP_LWS << std::endl; std::cout << "CT_LWS = " << CT_LWS << std::endl; std::cout << "USE_ALL_DEVICES = " << USE_ALL_DEVICES << std::endl << std::endl; #else std::cout << "device_count = " << device_count << std::endl << std::endl; #endif #ifdef _WIN32 rng.seed(); srand(GetTickCount()); #else rng.seed(); srand(getTimeMs()); #endif u = boost::uniform_real<float>(0.0f, 1000000.0f); gen = new boost::variate_generator<boost::mt19937&, boost::uniform_real<float> >(rng, u); #ifdef USE_OPENCL cl_int clError = CL_SUCCESS; initCL(); for (int i = 0; i < clDevices.size(); ++i) { clInputBuf.push_back(cl::Buffer(clContext, CL_MEM_READ_ONLY, N * DIM * sizeof(float), NULL, &clError)); if (clError != CL_SUCCESS) std::cout << "OpenCL Error: Could not create buffer" << std::endl; clCentroidBuf.push_back(cl::Buffer(clContext, CL_MEM_READ_WRITE, K * DIM * sizeof(float), NULL, &clError)); if (clError != CL_SUCCESS) std::cout << "OpenCL Error: Could not create buffer" << std::endl; clMappingBuf.push_back(cl::Buffer(clContext, CL_MEM_READ_WRITE, N * sizeof(int), NULL, &clError)); if (clError != CL_SUCCESS) std::cout << "OpenCL Error: Could not create buffer" << std::endl; clReductionBuf.push_back(cl::Buffer(clContext, CL_MEM_WRITE_ONLY, N * sizeof(float), NULL, &clError)); if (clError != CL_SUCCESS) std::cout << "OpenCL Error: Could not create buffer" << std::endl; clClusterAssignment[i].setArgs(clInputBuf[i](), clCentroidBuf[i](), clMappingBuf[i]()); clClusterReposition[i].setArgs(clInputBuf[i](), clMappingBuf[i](), clCentroidBuf[i]()); clClusterReposition_k[i].setArgs(clInputBuf[i](), clMappingBuf[i](), clCentroidBuf[i]()); //clClusterReposition_k_c[i].setArgs(clInputBuf[i](), clMappingBuf[i](), clCentroidBuf[i](), clConvergedBuf[i]()); clComputeCost[i].setArgs(clInputBuf[i](), clCentroidBuf[i](), clMappingBuf[i](), clReductionBuf[i]()); } device_count = clDevices.size(); #endif util::Clock clock; clock.reset(); for (int i = 0; i < RUNS; ++i) { mapping_list.push_back(NULL); centroids_list.push_back(NULL); cost_list.push_back(0.0f); } float* source = new float[N*DIM]; for (int i = 0; i < N*DIM; ++i) source[i] = gen_random_float(); input_list.push_back(source); for (int i = 1; i < device_count; ++i) { float* copy = new float[N*DIM]; memcpy(copy, source, N*DIM*sizeof(float)); input_list.push_back(copy); } if (device_count > 1) { boost::thread_group threads; for (int i = 0; i < device_count; ++i) { threads.create_thread(boost::bind(exec, i, true)); } threads.join_all(); } else { exec(0, false); } #ifdef USE_OPENCL reduction_group.join_all(); #endif int best_result = 0; float best_cost = std::numeric_limits<float>::max(); for (int i = 0; i < RUNS; ++i) { if (cost_list[i] < best_cost) { best_cost = cost_list[i]; best_result = i; } } FILE *out_fdesc = fopen("centroids.out", "wb"); fwrite((void*)centroids_list[best_result], K * DIM * sizeof(float), 1, out_fdesc); fclose(out_fdesc); out_fdesc = fopen("mapping.out", "wb"); fwrite((void*)mapping_list[best_result], N * sizeof(int), 1, out_fdesc); fclose(out_fdesc); std::cout << "Best result is " << best_result << std::endl; for (int i = 0; i < device_count; ++i) { delete[] input_list[i]; } for (int i = 0; i < RUNS; ++i) { delete[] mapping_list[i]; delete[] centroids_list[i]; } float now = clock.get(); std::cout << "Total: " << now << std::endl; system("pause"); return 0; }
void ParticleDemo::setupScene(const ConstructionInfo& ci) { initCL(ci.preferredOpenCLDeviceIndex,ci.preferredOpenCLPlatformIndex); int numParticles = NUM_PARTICLES_X*NUM_PARTICLES_Y*NUM_PARTICLES_Z; int maxObjects = NUM_PARTICLES_X*NUM_PARTICLES_Y*NUM_PARTICLES_Z+1024; int maxPairsSmallProxy = 32; float radius = 3.f*m_data->m_simParamCPU[0].m_particleRad; m_data->m_broadphaseGPU = new b3GpuSapBroadphase(m_clData->m_clContext ,m_clData->m_clDevice,m_clData->m_clQueue);//overlappingPairCache,b3Vector3(4.f, 4.f, 4.f), 128, 128, 128,maxObjects, maxObjects, maxPairsSmallProxy, 100.f, 128, /*m_data->m_broadphaseGPU = new b3GridBroadphaseCl(overlappingPairCache,b3Vector3(radius,radius,radius), 128, 128, 128, maxObjects, maxObjects, maxPairsSmallProxy, 100.f, 128, m_clData->m_clContext ,m_clData->m_clDevice,m_clData->m_clQueue); */ m_data->m_velocitiesGPU = new b3OpenCLArray<b3Vector3>(m_clData->m_clContext,m_clData->m_clQueue,numParticles); m_data->m_velocitiesCPU.resize(numParticles); for (int i=0;i<numParticles;i++) { m_data->m_velocitiesCPU[i].setValue(0,0,0); } m_data->m_velocitiesGPU->copyFromHost(m_data->m_velocitiesCPU); m_data->m_simParamGPU = new b3OpenCLArray<b3SimParams>(m_clData->m_clContext,m_clData->m_clQueue,1,false); m_data->m_simParamGPU->copyFromHost(m_data->m_simParamCPU); cl_int pErrNum; cl_program prog = b3OpenCLUtils::compileCLProgramFromString(m_clData->m_clContext,m_clData->m_clDevice,particleKernelsString,0,"",INTEROPKERNEL_SRC_PATH); m_data->m_updatePositionsKernel = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "updatePositionsKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_data->m_updatePositionsKernel2 = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "integrateMotionKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_data->m_updateAabbsKernel= b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "updateAabbsKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_data->m_collideParticlesKernel = b3OpenCLUtils::compileCLKernelFromString(m_clData->m_clContext, m_clData->m_clDevice,particleKernelsString, "collideParticlesKernel" ,&pErrNum,prog); oclCHECKERROR(pErrNum, CL_SUCCESS); m_instancingRenderer = ci.m_instancingRenderer; int strideInBytes = 9*sizeof(float); bool pointSprite = true; int shapeId =-1; if (pointSprite) { int numVertices = sizeof(point_sphere_vertices)/strideInBytes; int numIndices = sizeof(point_sphere_indices)/sizeof(int); shapeId = m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS); } else { int numVertices = sizeof(low_sphere_vertices)/strideInBytes; int numIndices = sizeof(low_sphere_indices)/sizeof(int); shapeId = m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices); } float position[4] = {0,0,0,0}; float quaternion[4] = {0,0,0,1}; float color[4]={1,0,0,1}; float scaling[4] = {0.023,0.023,0.023,1}; int userIndex = 0; for (int x=0;x<NUM_PARTICLES_X;x++) { for (int y=0;y<NUM_PARTICLES_Y;y++) { for (int z=0;z<NUM_PARTICLES_Z;z++) { float rad = m_data->m_simParamCPU[0].m_particleRad; position[0] = x*(rad*3); position[1] = y*(rad*3); position[2] = z*(rad*3); color[0] = float(x)/float(NUM_PARTICLES_X); color[1] = float(y)/float(NUM_PARTICLES_Y); color[2] = float(z)/float(NUM_PARTICLES_Z); int id = m_instancingRenderer->registerGraphicsInstance(shapeId,position,quaternion,color,scaling); void* userPtr = (void*)userIndex; int collidableIndex = userIndex; b3Vector3 aabbMin,aabbMax; b3Vector3 particleRadius(rad,rad,rad); aabbMin = b3Vector3(position[0],position[1],position[2])-particleRadius; aabbMax = b3Vector3(position[0],position[1],position[2])+particleRadius; m_data->m_broadphaseGPU->createProxy(aabbMin,aabbMax,collidableIndex,1,1); userIndex++; } } } m_data->m_broadphaseGPU->writeAabbsToGpu(); float camPos[4]={1.5,0.5,2.5,0}; m_instancingRenderer->setCameraTargetPosition(camPos); m_instancingRenderer->setCameraDistance(4); m_instancingRenderer->writeTransforms(); }
/*---< main() >-------------------------------------------------------------*/ int setup(int argc, char **argv) { int opt; extern char *optarg; char *filename = 0; float *buf; char line[1024]; int isBinaryFile = 0; float threshold = 0.001; /* default value */ int max_nclusters=5; /* default value */ int min_nclusters=5; /* default value */ int best_nclusters = 0; int nfeatures = 0; int npoints = 0; float len; float **features; float **cluster_centres=NULL; int i, j, index; int nloops = 1; /* default value */ int isRMSE = 0; float rmse; int isOutput = 0; //float cluster_timing, io_timing; //ocd_init(&argc, &argv, NULL); //ocd_options opts = ocd_get_options(); //platform_id = opts.platform_id; //device_id = opts.device_id; /* obtain command line arguments and change appropriate options */ while ( (opt=getopt(argc,argv,"i:t:m:n:l:bro"))!= EOF) { switch (opt) { case 'i': filename=optarg; break; case 'b': isBinaryFile = 1; break; case 't': threshold=atof(optarg); break; case 'm': max_nclusters = atoi(optarg); break; case 'n': min_nclusters = atoi(optarg); break; case 'r': isRMSE = 1; break; case 'o': isOutput = 1; break; case 'l': nloops = atoi(optarg); break; case '?': usage(argv[0]); break; default: usage(argv[0]); break; } } if (filename == 0) usage(argv[0]); /* ============== I/O begin ==============*/ /* get nfeatures and npoints */ //io_timing = omp_get_wtime(); if (isBinaryFile) { //Binary file input int infile; if ((infile = open(filename, O_RDONLY, "0600")) == -1) { fprintf(stderr, "Error: no such file (%s)\n", filename); exit(1); } read(infile, &npoints, sizeof(int)); read(infile, &nfeatures, sizeof(int)); /* allocate space for features[][] and read attributes of all objects */ buf = (float*) memalign(AOCL_ALIGNMENT,npoints*nfeatures*sizeof(float)); features = (float**)memalign(AOCL_ALIGNMENT,npoints* sizeof(float*)); features[0] = (float*) memalign(AOCL_ALIGNMENT,npoints*nfeatures*sizeof(float)); for (i=1; i<npoints; i++) features[i] = features[i-1] + nfeatures; read(infile, buf, npoints*nfeatures*sizeof(float)); close(infile); } else { FILE *infile; if ((infile = fopen(filename, "r")) == NULL) { fprintf(stderr, "Error: no such file (%s)\n", filename); exit(1); } while (fgets(line, 1024, infile) != NULL) if (strtok(line, " \t\n") != 0) npoints++; rewind(infile); while (fgets(line, 1024, infile) != NULL) { if (strtok(line, " \t\n") != 0) { /* ignore the id (first attribute): nfeatures = 1; */ while (strtok(NULL, " ,\t\n") != NULL) nfeatures++; break; } } /* allocate space for features[] and read attributes of all objects */ buf = (float*) memalign(AOCL_ALIGNMENT,npoints*nfeatures*sizeof(float)); features = (float**)memalign(AOCL_ALIGNMENT,npoints* sizeof(float*)); features[0] = (float*) memalign(AOCL_ALIGNMENT,npoints*nfeatures*sizeof(float)); for (i=1; i<npoints; i++) features[i] = features[i-1] + nfeatures; rewind(infile); i = 0; while (fgets(line, 1024, infile) != NULL) { if (strtok(line, " \t\n") == NULL) continue; for (j=0; j<nfeatures; j++) { buf[i] = atof(strtok(NULL, " ,\t\n")); i++; } } fclose(infile); } //io_timing = omp_get_wtime() - io_timing; printf("\nI/O completed\n"); printf("\nNumber of objects: %d\n", npoints); printf("Number of features: %d\n", nfeatures); /* ============== I/O end ==============*/ // error check for clusters if (npoints < min_nclusters) { printf("Error: min_nclusters(%d) > npoints(%d) -- cannot proceed\n", min_nclusters, npoints); exit(0); } srand(7); /* seed for future random number generator */ memcpy(features[0], buf, npoints*nfeatures*sizeof(float)); /* now features holds 2-dimensional array of features */ free(buf); /* ============ Initialize OpenCL Environment ============ */ initCL(); /* ======================= core of the clustering ===================*/ //cluster_timing = omp_get_wtime(); /* Total clustering time */ cluster_centres = NULL; index = cluster(npoints, /* number of data points */ nfeatures, /* number of features for each point */ features, /* array: [npoints][nfeatures] */ min_nclusters, /* range of min to max number of clusters */ max_nclusters, threshold, /* loop termination factor */ &best_nclusters, /* return: number between min and max */ &cluster_centres, /* return: [best_nclusters][nfeatures] */ &rmse, /* Root Mean Squared Error */ isRMSE, /* calculate RMSE */ nloops); /* number of iteration for each number of clusters */ //cluster_timing = omp_get_wtime() - cluster_timing; /* =============== Command Line Output =============== */ /* cluster center coordinates :displayed only for when k=1*/ if((min_nclusters == max_nclusters) && (isOutput == 1)) { printf("\n================= Centroid Coordinates =================\n"); for(i = 0; i < max_nclusters; i++){ printf("%d:", i); for(j = 0; j < nfeatures; j++){ printf(" %.2f", cluster_centres[i][j]); } printf("\n\n"); } } len = (float) ((max_nclusters - min_nclusters + 1)*nloops); //printf("Number of Iteration: %d\n", nloops); //printf("Time for I/O: %.5fsec\n", io_timing); //printf("Time for Entire Clustering: %.5fsec\n", cluster_timing); if(min_nclusters != max_nclusters){ if(nloops != 1){ //range of k, multiple iteration //printf("Average Clustering Time: %fsec\n", // cluster_timing / len); printf("Best number of clusters is %d\n", best_nclusters); } else{ //range of k, single iteration //printf("Average Clustering Time: %fsec\n", // cluster_timing / len); printf("Best number of clusters is %d\n", best_nclusters); } } else{ if(nloops != 1){ // single k, multiple iteration //printf("Average Clustering Time: %.5fsec\n", // cluster_timing / nloops); if(isRMSE) // if calculated RMSE printf("Number of trials to approach the best RMSE of %.3f is %d\n", rmse, index + 1); } else{ // single k, single iteration if(isRMSE) // if calculated RMSE printf("Root Mean Squared Error: %.3f\n", rmse); } } /* free up memory */ #ifndef __FPGA__ free(features[0]); free(features); #endif return(0); }