IOChannel * SockDgram::createChannel(int IOCHANNEL_TYPE, void * pAddr) { if(pAddr == NULL) return NULL; //In Socket Datagram, whether its connection is active or not is not important. if(IOCHANNEL_TYPE == IOCHANNEL_OPEN_ACTIVE) { } else if(IOCHANNEL_TYPE == IOCHANNEL_OPEN_PASIVE) { } PeerAddr_[connectedPeer_] = *(os_sockaddr_in *)pAddr; os_sockaddr_in *fromAddr = &PeerAddr_[connectedPeer_++]; MW_DEBUG((LM_DEBUG,"*************************************************\n")); MW_DEBUG((LM_DEBUG,"[UDP] NEW CHANNEL IS CREATED FOR ADDR!\n")); MW_DEBUG((LM_DEBUG,"[UDP] <Remote Peer - IP:%s Port:%d>\n", // inet_ntoa(PeerAddr.sin_addr), ntohs(PeerAddr.sin_port))); inet_ntoa(fromAddr->sin_addr), ntohs(fromAddr->sin_port))); MW_DEBUG((LM_DEBUG,"*************************************************\n")); IOChannel *pIOC = new SockDgram_IOChannel(fromAddr, this); ctChannelList.insert(std::make_pair(fromAddr, pIOC)); return pIOC; }
void SockDgram_AsyncOpeartion::handleUserDefinedSystemEvent(OSAL_SOCKET sd) { os_sockaddr_in fromAddr; DEBUGTRACE("[UDP Server] SockDgram_AsyncOpeartion: run!"); Msg_t ReadBuf; ReadBuf.pData_ = incomingMsg_.pData_; MW_DEBUG((LM_DEBUG,"[UDP Server] SockDgram_AsyncOpeartion: Ready to receive data!\n\n")); #ifdef OSAL_WIN32 int nRead = pOwnDevice_->read(&incomingMsg_, NULL, (os_sockaddr*)&fromAddr); #else int nRead = pOwnDevice_->read(&incomingMsg_, NULL, &fromAddr); #endif MW_DEBUG((LM_DEBUG,"[UDP Server] From IP address=%s, Port number=%d\n", inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port))); if(nRead <= 0) { MW_DEBUG((LM_DEBUG,"[UDP Server] SockDgram_AsyncOpeartion: Error!\n")); perror("SockDgram Read Error:"); } else { MW_DEBUG((LM_DEBUG,"[UDP Server] SockDgram_AsyncOpeartion: dispatch!\n")); ReadBuf.len_= nRead; #ifdef OSAL_WIN32 pOwnDevice_->dispatch(&fromAddr, nRead, ACT_READEVENT); #else pOwnDevice_->dispatch(&fromAddr, &ReadBuf, ACT_READEVENT); #endif } }
void integral_compose(double* integral_results, int num_results, double* results, ASTRONOMY_PARAMETERS* ap) { int i, j, current; results[0] = 0.0; for (i = 0; i < ap->number_streams; i++) { results[i+1] = 0.0; } for (i = 0; i < num_results; i++) { current = (ap->number_streams + 1) * i; results[0] += integral_results[current]; for (j = 0; j < ap->number_streams; j++) { results[j+1] += integral_results[current + j + 1]; } } #ifdef MW_ENABLE_DEBUG MW_DEBUG("background integral: %lf, stream integrals:", results[0]); for (i = 0; i < ap->number_streams; i++) printf(" %lf", results[i+1]); printf("\n"); #endif /* MW_ENABLE_DEBUG */ }
void integral_f(double* parameters, double* results, ASTRONOMY_PARAMETERS* ap, EVALUATION_STATE* es, STAR_POINTS* sp) { int i, retval; set_astronomy_parameters(ap, parameters); reset_evaluation_state(es); retval = calculate_integrals(ap, es, sp); if (retval) { fprintf(stderr, "APP: error calculating integrals: %d\n", retval); exit(retval); } results[0] = es->background_integral; for (i = 0; i < ap->number_streams; i++) { results[i+1] = es->stream_integrals[i]; } #ifdef MW_ENABLE_DEBUG MW_DEBUG("background integral: %lf, stream integrals:", results[0]); for (i = 0; i < ap->number_streams; i++) printf(" %lf", results[i+1]); printf("\n"); #endif /* MW_ENABLE_DEBUG */ }
THR_Handle_t ThreadManager::startThread(OSAL::IRunnable &target) { //TODO:This routine shall be changed into thread pool uint32 ThreadId; THR_Handle_t hThread; hThread = OSAL::createThread(NULL, 0, ThreadRoutine, (void *)&target, 0, &ThreadId); static int cnt = 0; if(hThread == NULL) { MW_DEBUG((LM_DEBUG,"Starting Thread is failed.\n")); OSAL::TerminateThread(hThread, -1); } MW_DEBUG((LM_DEBUG,"Thread Start #Thread(%d) \n",++cnt)); return hThread; }
/* //Blocking Read operation int SockDgram_IOChannel::read(Msg_t *msg, sockaddr_in *fromAddr ) { return pOwnDevice_->read(msg, NULL, fromAddr); } */ int SockDgram_IOChannel::dataRequest(void * arg, Msg_t *msg) { #ifdef OSAL_WIN32 return pOwnDevice_->write(msg, NULL, (SOCKADDR*)getDstAddr()); #else MW_DEBUG((LM_DEBUG,"[UDP] Send message to IP address=%s, Port number=%d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port))); return pOwnDevice_->write(msg, NULL, &remote); #endif }
void SockStream_SyncOpeartion::run() { DEBUGTRACE("SockStream_SyncOpeartion::run"); while(pOwnDevice_->getState() == IODevice::IODEV_ACTIVE) { MW_DEBUG((LM_DEBUG,"SockStream_SyncOpeartion: Ready to receive data!\n")); } pOwnDevice_->close(); }
void SockDgram_IOChannel_Thread::run() { sockaddr_in fromAddr; Msg_t incomingBuf, ReadBuf; memset(&fromAddr,0, sizeof(sockaddr_in)); incomingBuf.len_ = pSockDgram_IOChannel_->getSizeofBuffer(); incomingBuf.pData_ = ReadBuf.pData_ = pSockDgram_IOChannel_->getBuffer(); int nRead = 0; DEBUGTRACE("SockDgram_SyncOpeartion: run!"); while((pSockDgram_IOChannel_ != NULL) ) { MW_DEBUG((LM_DEBUG,"SockDgram_SyncOpeartion: Ready to receive data!\n\n")); // int nRead = pSockDgram_IOChannel_->read(&incomingBuf, &fromAddr); MW_DEBUG((LM_DEBUG,"[UDP] From IP address=%s, Port number=%d\n", inet_ntoa(fromAddr.sin_addr), ntohs(fromAddr.sin_port))); if(nRead > 0) { ReadBuf.len_= nRead; pSockDgram_IOChannel_->dataIndication(NULL, &ReadBuf); MW_DEBUG((LM_DEBUG,"SockDgram_IOChannel_Thread::Read data is succeeded nRead %d!\n",nRead)); } else { MW_DEBUG((LM_DEBUG,"SockDgram_IOChannel_Thread:: Error on reading data")); pSockDgram_IOChannel_->close(); break; } } }
double likelihood_compose(double* results, size_t num_results) { double prob_sum = 0.0; double bad_jacobians = 0.0; size_t i; for (i = 0; i < num_results; i++) { prob_sum += results[(2*i)]; bad_jacobians += results[(2*i)+1]; } prob_sum /= (total_number_stars - bad_jacobians); MW_DEBUG("[compose] likelihood: %.10lf\n", prob_sum); return prob_sum - 3.0; }
void likelihood_f(double* integrals, double* results, ASTRONOMY_PARAMETERS* ap, EVALUATION_STATE* es, STAR_POINTS* sp) { int i, retval; es->background_integral = integrals[0]; for (i = 0; i < ap->number_streams; i++) es->stream_integrals[i] = integrals[i+1]; retval = calculate_likelihood(ap, es, sp); if (retval) { fprintf(stderr, "APP: error calculating likelihood: %d\n", retval); exit(retval); } results[0] = es->prob_sum; results[1] = es->bad_jacobians; MW_DEBUG("calculated likelihood: %lf, bad_jacobs: %lf\n", results[0], results[1]); }
int SockDgram::doOpen(void *fileDescriptor) { DEBUGTRACE("SockDgram::doOpen"); //read configuration file OSAL_UNUSED_ARG(fileDescriptor); //set operation mode this->setOpMode(pSysEventProcessor_->getOpMode()); if(this->getOpMode() == OSAL::OPMODE_ASYNC) { //pfRead_ = SockDgram::asyncRead; //pfWrite_ = SockDgram::asyncWrite; pfRead_ = SockDgram::read; pfWrite_ = SockDgram::write; #ifdef OSAL_WIN32 if(this->openAsyncSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED, _ACCEPT_USE_) == INVALID_SOCKET) { return INVALID_SOCKET; } #else pOlReadACT_ = NULL; pOlWriteACT_= NULL; if(this->socket(AF_INET, SOCK_DGRAM, 0) == -1) { perror("Failed to open socket"); return -1; } int m_sockfd = this->getSD(); int flag = fcntl(m_sockfd, F_GETFL, 0); // socket status flags fcntl(m_sockfd, F_SETFL, flag | O_NONBLOCK); #endif } else /* Synchronous mode*/ { pfRead_ = SockDgram::read; pfWrite_ = SockDgram::write; //create socket if(this->openSyncSocket(AF_INET, SOCK_DGRAM, 0) == -1) { return -1; } } //set socket options #ifdef OSAL_WIN32 BOOL bOptVal = TRUE; int bOptLen = sizeof(BOOL); SocketControlParam_t scp[] = { {this->getSD(_ACCEPT_USE_), SOL_SOCKET, MW_SO_BROADCAST, (char*)&bOptVal, &bOptLen}, }; #else int one = 1; SocketControlParam_t scp[] = { {this->getSD(), SOL_SOCKET, SO_BROADCAST, &one}, }; #endif if(control(SETPARAMS, scp) == -1 ) { MW_ERRMSG((LM_DEBUG,"SockDgram::Broadcast is not enabled\n")); return -1; } //bind socket if(this->bind(ConfigParam_.ipAddr_, ConfigParam_.port_) != MW_SUCCESS) return MW_ERROR; MW_DEBUG((LM_DEBUG,"[Dev:Socket Dgram] UDP Service is ready to start!\n")); return MW_SUCCESS; }
/* Returns the newly allocated array of parameters */ static double* parse_parameters(int argc, const char** argv, int* paramnOut) { poptContext context; int o; unsigned int i, paramn = 0; double* parameters = NULL; static const char** rest; static const struct poptOption options[] = { { "boinc-init-graphics", 'b', POPT_ARG_STRING, &boinc_graphics, 'b', "Argument to boinc_init_graphics", NULL }, { "search-parameter-file", 's', POPT_ARG_STRING, &search_parameter_file, 's', "Search parameter file name", NULL }, { "star-points-file", 'p', POPT_ARG_STRING, &star_points_file, 'p', "Star points files", NULL }, { "astronomy-parameter-file", 'a', POPT_ARG_STRING, &astronomy_parameter_file, 'a', "Astronomy parameter file", NULL }, { "output", 'o', POPT_ARG_STRING, &output_file, 'o', "Output file", NULL }, POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL } }; context = poptGetContext(argv[0], argc, argv, options, POPT_CONTEXT_POSIXMEHARDER); while ( ( o = poptGetNextOpt(context)) >= 0 ); if ( o < -1 ) { poptPrintHelp(context, stderr, 0); mw_finish(EXIT_FAILURE); } MW_DEBUG("Got arguments: " "boinc_graphics = '%s' " "search_parameter_file = '%s' " "star_points_file = '%s' " "astronomy_parameter_file = '%s' " "output_file = '%s'\n", boinc_graphics, search_parameter_file, star_points_file, astronomy_parameter_file, output_file); rest = poptGetArgs(context); if (rest) { while (rest[++paramn]); /* Count number of parameters */ MW_DEBUG("%u arguments leftover\n", paramn); parameters = (double*) malloc(sizeof(double) * paramn); errno = 0; for ( i = 0; i < paramn; ++i ) { parameters[i] = strtod(rest[i], NULL); if (errno) { perror("error parsing command line parameters"); poptPrintHelp(context, stderr, 0); free(parameters); poptFreeContext(context); mw_finish(EXIT_FAILURE); } } } poptFreeContext(context); *paramnOut = paramn; return parameters; }
static void worker(int argc, const char** argv) { double* parameters; int ret1, ret2; int number_parameters, ap_number_parameters; ASTRONOMY_PARAMETERS ap = { 0 }; STAR_POINTS sp = { 0 }; EVALUATION_STATE es = { 0 }; parameters = parse_parameters(argc, argv, &number_parameters); if (!parameters) { fprintf(stderr, "Could not parse parameters from the command line\n"); mw_finish(EXIT_FAILURE); } ret1 = read_astronomy_parameters(astronomy_parameter_file, &ap); ret2 = read_star_points(star_points_file, &sp); MW_DEBUG("ap.number_stream_parameters = %d\n", ap.number_stream_parameters); if (ret1) { fprintf(stderr, "APP: error reading astronomy parameters from file %s: %d\n", astronomy_parameter_file, ret1); } if (ret2) { fprintf(stderr, "APP: error reading star points from file %s: %d\n", star_points_file, ret2); } if (ret1 | ret2) { free(parameters); cleanup_worker(); mw_finish(EXIT_FAILURE); } initialize_state(&ap, &sp, &es); ap_number_parameters = get_optimized_parameter_count(&ap); if (number_parameters < 1 || number_parameters != ap_number_parameters) { fprintf(stderr, "Error reading parameters: number of parameters from the " "command line (%d) does not match the number of parameters " "to be optimized in %s (%d)\n", number_parameters, astronomy_parameter_file, ap_number_parameters); free(parameters); cleanup_worker(); mw_finish(EXIT_FAILURE); } set_astronomy_parameters(&ap, parameters); #if COMPUTE_ON_CPU init_constants(&ap); init_simple_evaluator(cpu_evaluate); #elif USE_CUDA init_constants(&ap); init_simple_evaluator(cuda_evaluate); #elif USE_OCL init_constants(&ap); init_simple_evaluator(ocl_evaluate); #else #error "Must choose CUDA, OpenCL or CPU" #endif /* COMPUTE_ON_CPU */ /* CHECKME: What is this magic 3.0, and why was it being * subtracted from CPU and CUDA result, but not OpenCL? */ double likelihood = evaluate(parameters, &ap, &es, &sp) - 3.0; fprintf(stderr, "<search_likelihood> %0.20f </search_likelihood>\n", likelihood); fprintf(stderr, "<search_application> %s %s </search_application>\n", BOINC_APP_VERSION, PRECISION); free(parameters); cleanup_worker(); mw_finish(EXIT_SUCCESS); }
/* stPsgFunction - determines the probability that a star with given coordinates (coordpar, in lbr format) is a part of a stream defined by the given parameters (pars). It is assumed that lbr coordinates are solar-centered and xyz coordinates are galactic centered. verb flags the function to output it's work as it executes. Return: a double value is returned indicating the probability that the star is in the stream. A higher value indicates a higher probability. If a value < 0 is returned, an error occured. -1 - a parameters is NaN -2 - an error occured in the call to lbr2stream */ double stPsgFunction(const double* coordpar, const double* spars, int wedge, int sgr_coordinates) { //update: allow for new coordinate transforms double xyz[3], lbr[3], a[3], c[3]; double mu, r, theta, phi, sigma; double dotted, xyz_norm, prob; double ra, dec, lamda, beta, l, b; mu = spars[0]; r = spars[1]; theta = spars[2]; phi = spars[3]; sigma = spars[4]; //update: convert from mu, nu, r geometry to a and c geometry if (sgr_coordinates == 0) { atGCToEq(mu, 0, &ra, &dec, get_node(), wedge_incl(wedge)); atEqToGal(ra, dec, &l, &b); } else if (sgr_coordinates == 1) { gcToSgr(mu, 0, wedge, &lamda, &beta); sgrToGal(lamda, beta, &l, &b); // <<<make sure the conversion is correct (check with conversiontester.vb)>>> MW_DEBUG(" wedge=%i, mui=%f, nui=0, lamda=%f, beta=%f, l=%f, b=%f", wedge, mu, lamda, beta, l, b); } else { fprintf(stderr, "Error: sgr_coordinates not valid"); } lbr[0] = l; lbr[1] = b; lbr[2] = r; lbr2xyz(lbr, c); a[0] = sin(theta) * cos(phi); a[1] = sin(theta) * sin(phi); a[2] = cos(theta); //Sigma near 0 so star prob is 0. if (sigma > -0.0001 && sigma < 0.0001) return 0; lbr2xyz(coordpar, xyz); xyz[0] = xyz[0] - c[0]; xyz[1] = xyz[1] - c[1]; xyz[2] = xyz[2] - c[2]; dotted = dotp(a, xyz); xyz[0] = xyz[0] - dotted * a[0]; xyz[1] = xyz[1] - dotted * a[1]; xyz[2] = xyz[2] - dotted * a[2]; xyz_norm = norm(xyz); MW_DEBUG("dotted: %lf, xyz_norm: %lf, sigma: %lf\n", dotted, xyz_norm, sigma); prob = exp( -(xyz_norm * xyz_norm) / 2 / (sigma * sigma) ); MW_DEBUG("prob before ref: %lf\n", prob); return prob; }
void separation(const char* filename, double background_integral, double* stream_integrals) { int q[ap->number_streams]; double nstars[ap->number_streams]; int total; double sprob[ap->number_streams]; double prob_s[ap->number_streams]; double prob_b; double pbx; double psg[ap->number_streams]; double d; int twoPanel; double** cmatrix; double dnormal[3]; double dortho[3]; double xsun[3]; double epsilon_s[ap->number_streams]; double epsilon_b; double star_coords[3]; double starxyz[3]; double starxyzTransform[3]; int s_ok = 0; int i, j, retval; FILE* file; double reff_xr_rp3, *qw_r3_N, *r_point; twoPanel = 1; for (j = 0; j < ap->number_streams; j++) { nstars[j] = 0; q[j] = 0; } total = 0; prob_ok_init(); printf("Integral complete.\n Beginning probability calculations...\n"); file = fopen(filename, "w"); if (ap->sgr_coordinates == 0) { stripe_normal(ap->wedge, dnormal); } else if (ap->sgr_coordinates == 1) { sgr_stripe_normal(ap->wedge, dnormal); } else { printf("Error: ap->sgr_coordinates not valid"); } free_star_points(sp); free(sp); sp = (STAR_POINTS*)malloc(sizeof(STAR_POINTS)); retval = read_star_points(star_points_file, sp); if (retval) { fprintf(stderr, "APP: error reading star points: %d\n", retval); exit(1); } printf("read %d stars.\n", sp->number_stars); cmatrix = (double**)malloc(sizeof(double*) * 3); for (i = 0; i < 3; i++) cmatrix[i] = (double*)malloc(sizeof(double) * 3); dortho[0] = 0.0; dortho[1] = 0.0; dortho[2] = 1.0; get_transform(dnormal, dortho, cmatrix); printf("\nTransformation matrix:\n"); printf("\t%lf %lf %lf\n", cmatrix[0][0], cmatrix[0][1], cmatrix[0][2]); printf("\t%lf %lf %lf\n", cmatrix[1][0], cmatrix[1][1], cmatrix[1][2]); printf("\t%lf %lf %lf\n\n", cmatrix[2][0], cmatrix[2][1], cmatrix[2][2]); xsun[0] = -8.5; xsun[1] = 0.0; xsun[2] = 0.0; d = dotp(dnormal, xsun); printf("==============================================\n"); printf("bint: %lf", background_integral); for (j = 0; j < ap->number_streams; j++) { printf(", "); printf("sint[%d]: %lf", j, stream_integrals[j]); } printf("\n"); /*get stream & background weight constants*/ double denom = 1.0; for (j = 0; j < ap->number_streams; j++) { denom += exp(ap->stream_weights[j]); } for (j = 0; j < ap->number_streams; j++) { epsilon_s[j] = exp(ap->stream_weights[j]) / denom; printf("epsilon_s[%d]: %lf\n", j, epsilon_s[j]); } epsilon_b = 1.0 / denom; printf("epsilon_b: %lf\n", epsilon_b); r_point = (double*)malloc(sizeof(double) * ap->convolve); qw_r3_N = (double*)malloc(sizeof(double) * ap->convolve); init_constants(ap); printf("initialized constants\n"); for (i = 0; i < sp->number_stars; i++) { MW_DEBUG("[%d/%d] setting star coords\n", i, sp->number_stars); star_coords[0] = sp->stars[i][0]; star_coords[1] = sp->stars[i][1]; star_coords[2] = sp->stars[i][2]; MW_DEBUG("star_coords: %g %g %g\n", star_coords[0], star_coords[1], star_coords[2]); MW_DEBUG("twoPanel: %d\n", twoPanel); if (twoPanel == 1) { MW_DEBUG("setting probability constants\n"); set_probability_constants(ap->convolve, star_coords[2], r_point, qw_r3_N, &reff_xr_rp3); MW_DEBUG("calculating probabilities\n"); calculate_probabilities(r_point, qw_r3_N, reff_xr_rp3, star_coords, ap, &prob_b, prob_s); MW_DEBUG("calculated probabilities\n"); MW_DEBUG("prob_s: %lf\n", prob_s[0]); MW_DEBU("prob_b: %lf\n", prob_b); pbx = epsilon_b * prob_b / background_integral; for (j = 0; j < ap->number_streams; j++) { psg[j] = epsilon_s[j] * prob_s[j] / stream_integrals[j]; } MW_DEBUG("pbx: %g\n", pbx); MW_DEBUG("psg: %g\n", psg[0]); double psgSum = 0; for (j = 0; j < ap->number_streams; j++) { psgSum += psg[j]; } for (j = 0; j < ap->number_streams; j++) { sprob[j] = psg[j] / (psgSum + pbx); } MW_DEBUG("sprob: %g\n", sprob[0]); for (j = 0; j < ap->number_streams; j++) { nstars[j] += sprob[j]; } MW_DEBUG("nstars: %g\n", nstars[0]); } else { for (j = 0; j < ap->number_streams; j++) { sprob[j] = 1.0; nstars[j] += 1.0; } } /*determine if star with sprob should be put into stream*/ //for(j = 0; j < ap->number_streams; j++) { s_ok = prob_ok(ap->number_streams, sprob); // if (s_ok == 1) { // s_ok += j; // break; // } //} MW_DEBUG("s_ok: %d\n", s_ok); if (s_ok >= 1) { q[s_ok-1]++; } lbr2xyz(star_coords, starxyz); transform_point(starxyz, cmatrix, xsun, starxyzTransform); fprintf(file, "%d %lf %lf %lf\n", s_ok, starxyzTransform[0], starxyzTransform[1], starxyzTransform[2]); //free(starxyz); //free(starxyzTransform); total += 1; if ( (total % 10000) == 0 ) printf("%d\n", total); } printf("%d total stars\n", total); for (j = 0; j < ap->number_streams; j++) { printf("%lf in stream[%d] (%lf%%)\n", nstars[j], j, (nstars[j] / total * 100)); } for (j = 0; j < ap->number_streams; j++) { printf("%d stars separated into stream\n", q[j]); } fclose(file); printf("Output written to: %s\n", filename); free(r_point); free(qw_r3_N); free_constants(ap); }