struct FunctionStatistic* addNewStat(void *funcAddr, uint64_t diffTime) { // Do s_stats pointers writing into the shared memory if it is not performed before if (!isSharedMemoryInited) initSharedMemory(); ////// if (s_statsCount >= STATS_LIMIT){ printf("Statistics buffer is full! Exiting\n"); exit(1); } struct FunctionStatistic* stat = (struct FunctionStatistic*)mmap(0, sizeof(struct FunctionStatistic), PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); //(struct FunctionStatistic*)malloc(sizeof(struct FunctionStatistic)); stat->realFuncAddr = funcAddr; stat->totalDiffTime = diffTime; stat->totalCallsNumber = 1; s_stats[ __sync_fetch_and_add(&s_statsCount, 1)] = stat; return stat; }
// WORKS FINE, but not when debugging int Service_Request(int conn) { struct ReqInfo reqinfo; InitReqInfo(&reqinfo); /* Get HTTP request */ if (Get_Request(conn, &reqinfo) < 0) return -1; else if(reqinfo.type == FULL) Output_HTTP_Headers(conn, &reqinfo); // file system: // Serve_Resource(ReqInfo reqinfo,int conn) CleanURL(reqinfo.resource); initSharedMemory(); // for each forked process! if(strlen(reqinfo.resource)>1000)return 0; char* q = substr(reqinfo.resource, 1, -1); // :::::::::::::::::::::::::::::: int ok=handle(q,conn); // <<<<<<< CENTRAL CALL // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FreeReqInfo(&reqinfo); return ok; }
extern "C" void test_lightShader(DataStruct* testData) { //read from texture and copy to shared memory int x_shared = __ptx_sreg_ntid_x*__ptx_sreg_ctaid_x*THREAD_WIDTH_X; int y_shared = __ptx_sreg_ntid_y*__ptx_sreg_ctaid_y*THREAD_WIDTH_Y+__ptx_sreg_tid_y*THREAD_WIDTH_Y; initSharedMemory((int*)testData->ia, (int*)testData->fa); const float Ka = 0.2f;//0.2 const float Kd = 0.6f;//0.4 const float Ks = 0.2f;//0.1 const float roughness = 0.1f; const float invRoughness = 1.0f/roughness; Color C_diffuse(0.0f, 0.0f, 0.0f); Color C_specular(0.0f, 0.0f, 0.0f); // BEGIN_ILLUMINANCE_LOOP for(int l=0; l<dataS.lights_n; l++) { Point P = getPosition(); Vector L_dir_norm = lightsS[l].position - P; float len_sq = Dot(L_dir_norm,L_dir_norm); float len = sqrtf(len_sq); L_dir_norm *= (1./len); //Normalize(L_dir_norm); //shadow? if(isShadowOld( lightsS[l].shadowTexture,//sampler2D shadowMap, P,//Vec3f viewPos, &(dataS.viewTransformInv),//Matrix4f invView, &(lightsS[l].shadowTransform),//Matrix4f shadowViewProj, lightsS[l].shadowFarClip, len,//distanceFromLight L_dir_norm, lightsS[l].direction )) { continue; } //recalculate // L_dir_norm = lightsS[l].position - getPosition(); // Normalize(L_dir_norm); //spotlight falloff and attenuation float spotlightAngle = clamp2One(Dot(lightsS[l].direction, -L_dir_norm)); float spotFalloff = clamp2One((+spotlightAngle - lightsS[l].spotlight_innerAngle) / (lightsS[l].spotlight_outerAngle - lightsS[l].spotlight_innerAngle)); float attenuation = Dot(lightsS[l].attenuation, Vector(1.0, len, len*len))/(1-spotFalloff); float cosLight; //diffuse component Point Nf = getNormalFF(); cosLight = Dot(L_dir_norm, Nf); //N if (cosLight >= 0.0) C_diffuse += lightsS[l].color_diffuse*cosLight/attenuation; //specular component L_dir_norm = L_dir_norm + Nf; //N Normalize(L_dir_norm); // L_dir_norm *= (1./Length(L_dir_norm)); cosLight = Dot(Nf, L_dir_norm); if(cosLight >= 0.0) C_specular += lightsS[l].color_diffuse * powf(cosLight, invRoughness)/attenuation;//Color(val, val, val); } Color result = (Ka + Kd * C_diffuse + Ks * C_specular); //load color of pixel from shared memory int index2 = __ptx_sreg_tid_x + (__ptx_sreg_tid_x/16)*16; float a2 = (float)*(double*)&(__ptx_shared_color[__ptx_sreg_tid_y*32*2*2+index2*2]); float b2 = (float)*(double*)&(__ptx_shared_color[__ptx_sreg_tid_y*32*2*2+index2*2+1]); index2 = 16 + (__ptx_sreg_tid_x+8)%16 + (__ptx_sreg_tid_x/16)*32; float c2 = (float)*(double*)&(__ptx_shared_color[__ptx_sreg_tid_y*32*2*2+index2*2]); float d2 = (float)*(double*)&(__ptx_shared_color[__ptx_sreg_tid_y*32*2*2+index2*2+1]); Color color = Color(a2, b2, c2); float specular = d2; result = color * result; clamp2One(result.x); clamp2One(result.y); clamp2One(result.z); int x2 = __ptx_sreg_ntid_x*__ptx_sreg_ctaid_x*THREAD_WIDTH_X+__ptx_sreg_tid_x*THREAD_WIDTH_X; int y2 = __ptx_sreg_ntid_y*__ptx_sreg_ctaid_y*THREAD_WIDTH_Y+__ptx_sreg_tid_y*THREAD_WIDTH_Y; int out = rgbaToInt(255.f*result.x,255.f*result.y,255.f*result.z,0); testData->i = out; }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { static bool firstTime = true; initSemaphore(&stepSEM); attachSEM(&stepSEM, STEP_SEM); initSharedMemory(&stepSHM, sizeof(episode_steps)); attachSHM(&stepSHM, STEP_SHM, &stepSEM); readSHMemory(&stepSHM, &episodeStep, &stepSEM); int numStates = (int) *mxGetPr(prhs[0]); int numSteps = episodeStep.numTransmittedSteps; fprintf(stderr, "NumSteps: %d\n",numSteps); plhs[0] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[1] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[2] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[3] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[4] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[5] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[6] = mxCreateDoubleMatrix(N_DOFS, numSteps, mxREAL); plhs[7] = mxCreateDoubleMatrix(7, numSteps, mxREAL); plhs[8] = mxCreateDoubleMatrix(numStates, numSteps, mxREAL); plhs[9] = mxCreateDoubleMatrix(1, numSteps, mxREAL); plhs[10] = mxCreateDoubleMatrix(1, numSteps, mxREAL); plhs[10] = mxCreateDoubleMatrix(1, numSteps, mxREAL); double *joints = mxGetPr(plhs[0]); double *jointsVel = mxGetPr(plhs[1]); double *jointsAcc = mxGetPr(plhs[2]); double *jointsDes = mxGetPr(plhs[3]); double *jointsVelDes = mxGetPr(plhs[4]); double *jointsAccDes = mxGetPr(plhs[5]); double *torque = mxGetPr(plhs[6]); double *cart = mxGetPr(plhs[7]); double *state = mxGetPr(plhs[8]); double *commandIdx = mxGetPr(plhs[9]); double *stepInTrajectory = mxGetPr(plhs[10]); double *doMotionIdx = mxGetPr(plhs[10]); memcpy(joints, episodeStep.joints, sizeof(double) * numSteps * N_DOFS); memcpy(jointsVel, episodeStep.jointsVel, sizeof(double) * numSteps * N_DOFS); memcpy(jointsAcc, episodeStep.jointsAcc, sizeof(double) * numSteps * N_DOFS); memcpy(jointsDes, episodeStep.jointsDes, sizeof(double) * numSteps * N_DOFS); memcpy(jointsVelDes, episodeStep.jointsVelDes, sizeof(double) * numSteps * N_DOFS); memcpy(jointsAccDes, episodeStep.jointsAccDes, sizeof(double) * numSteps * N_DOFS); memcpy(torque, episodeStep.torque, sizeof(double) * numSteps * N_DOFS); memcpy(cart, episodeStep.cart, sizeof(double) * numSteps * 7); // printf("doMotionIdx :"); // for (int i = 0; i < numSteps; i ++) // printf(" %d",episodeStep.doMotionIdx[i]); // printf(" \n"); for (int i = 0; i < numSteps; i ++) { for (int j = 0; j < numStates; j ++) { state[i * numStates + j] = episodeStep.state[i][j]; // printf("%f ", episodeStep.state[i][j]); } // printf("\n"); } for (int i = 0; i < numSteps; i ++) { commandIdx[i] = episodeStep.commandIdx[i]; doMotionIdx[i] = episodeStep.doMotionIdx[i]; } for (int i = 0; i < numSteps; i ++) { stepInTrajectory[i] = episodeStep.stepInTrajectory[i]; } deleteSemaphore(&stepSEM); deleteSharedMemory(&stepSHM); }