void* startPSO(void* tData){ int i, j; TData* myData = (TData*)tData; int id = myData->id; int numAnts = myData->numAnts; int function = myData->function; int mode = myData->mode; //init swarm Ant* swarm[numAnts]; /***START create ants and wait for all ants to be created***/ //lock ant creation for other threads pthread_mutex_lock(&mt); for(i = 0; i < numAnts; i++){ swarm[i] = initAnt(function); antsCreated++; } //init bestG if(bestG==NULL){ bestG = (BestGlobal*)malloc(sizeof(BestGlobal)); bestG->coos = (Point*)malloc(sizeof(Point)); Ant* tmp = swarm[0]; bestG->value = calFun(myData->function,tmp); bestG->coos->x = tmp->position->x; bestG->coos->y = tmp->position->y; } //unllock ant creation for other threads pthread_mutex_unlock(&mt); //wait for all ants to be created while(antsCreated<antsTotal){ pthread_cond_wait(&condition_var,&creation_mutex); } //all ants created -> wake all waiting threads if(antsCreated>=antsTotal-1){ pthread_cond_broadcast(&condition_var); } pthread_mutex_unlock(&creation_mutex); /*****END create ants and wait for all ants to be created*****/ //lock Global best access for other threads pthread_mutex_lock(&mt2); for(i = 0; i < numAnts; i++){ sem_wait(&findGlobalBestMutex); //enter and block access findGlobalBest(swarm[i], mode); //critical sem_post(&findGlobalBestMutex); //leave and realese access } threadsReady++; pthread_mutex_unlock(&mt2); //wait for all threads to find global while(threadsReady<numThreads){ pthread_cond_wait(&ready,&vel_mutex); } //all threads are ready -> wake threads if(threadsReady>= numThreads){ pthread_cond_broadcast(&ready); } pthread_mutex_unlock(&vel_mutex); for(i = 0; i < MAXITERATIONS; i++){ for(j = 0; j < numAnts; j++){ calVelocity(&swarm[j]); moveAnt(&(swarm[j]), function, mode); } for(j = 0; j < numAnts; j++){ findGlobalBest(swarm[j], mode); } } }
int main(int argc, char** argv) { LoadConfigFile(); initHMD(); glfwSetErrorCallback(ErrorCallback); if (!glfwInit()) { exit(EXIT_FAILURE); } glfwWindowHint(GLFW_DEPTH_BITS, 16); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);// : GLFW_OPENGL_COMPAT_PROFILE); //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #ifdef _DEBUG glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); #endif const std::string windowName = "RiftRay v" + std::string(pRiftRayVersion); GLFWwindow* l_Window = glfwCreateWindow(g_mirrorWindowSz.x, g_mirrorWindowSz.y, windowName.c_str(), NULL, NULL); if (!l_Window) { LOG_ERROR("Glfw failed to create a window. Exiting."); glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(l_Window); glfwSetKeyCallback(l_Window, keyboard); glfwSetMouseButtonCallback(l_Window, mouseDown); glfwSetCursorPosCallback(l_Window, mouseMove); glfwSetScrollCallback(l_Window, mouseWheel); glfwSetWindowSizeCallback(l_Window, resize); glfwSetInputMode(l_Window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); g_pMirrorWindow = l_Window; memset(m_keyStates, 0, GLFW_KEY_LAST*sizeof(int)); FindPreferredJoystick(); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { LOG_INFO("glewInit() error."); exit(EXIT_FAILURE); } #ifdef _DEBUG // Debug callback initialization // Must be done *after* glew initialization. glDebugMessageCallback(myCallback, NULL); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE); glDebugMessageInsert(GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_MARKER, 0, GL_DEBUG_SEVERITY_NOTIFICATION, -1 , "Start debugging"); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); #endif #ifdef USE_ANTTWEAKBAR TwInit(TW_OPENGL_CORE, NULL); initAnt(); #endif g_pScene = &g_gallery;// new ShaderGalleryScene(); if (g_pScene != NULL) { g_pScene->initGL(); } g_gallery.SetHmdPositionPointer(&m_hmdRo); g_gallery.SetHmdDirectionPointer(&m_hmdRd); g_gallery.SetChassisPosPointer(&m_chassisPos); g_gallery.SetChassisYawPointer(&m_chassisYaw); g_gallery.SetHeadSizePointer(&m_headSize); initVR(); StartShaderLoad(); glfwSwapInterval(0); while (!glfwWindowShouldClose(l_Window)) { glfwPollEvents(); joystick(); timestep(); #ifdef USE_ANTTWEAKBAR TwRefreshBar(g_pMainTweakbar); TwRefreshBar(g_pShaderTweakbar); #endif g_gallery.RenderPrePass(); displayHMD(); glfwSwapBuffers(l_Window); } exitVR(); g_pScene->exitGL(); glfwDestroyWindow(l_Window); glfwTerminate(); exit(EXIT_SUCCESS); }