/* Copy a memory block to VM physical RAM from real host */ void physmem_copy_to_vm(vm_instance_t *vm,void *real_buffer, m_uint64_t paddr,size_t len) { m_uint64_t dummy; m_uint32_t r; u_char *ptr; while(len > 0) { r = m_min(VM_PAGE_SIZE - (paddr & VM_PAGE_IMASK), len); ptr = physmem_get_hptr(vm,paddr,0,MTS_WRITE,&dummy); if (likely(ptr != NULL)) { memcpy(ptr,real_buffer,r); } else { r = m_min(len,4); switch(r) { case 4: physmem_copy_u32_to_vm(vm,paddr, htovm32(*(m_uint32_t *)real_buffer)); break; case 2: physmem_copy_u16_to_vm(vm,paddr, htovm16(*(m_uint16_t *)real_buffer)); break; case 1: physmem_copy_u8_to_vm(vm,paddr,*(m_uint8_t *)real_buffer); break; } } real_buffer += r; paddr += r; len -= r; } }
/* DMA transfer operation */ void physmem_dma_transfer(vm_instance_t *vm,m_uint64_t src,m_uint64_t dst, size_t len) { m_uint64_t dummy; u_char *sptr,*dptr; size_t clen,sl,dl; while(len > 0) { sptr = physmem_get_hptr(vm,src,0,MTS_READ,&dummy); dptr = physmem_get_hptr(vm,dst,0,MTS_WRITE,&dummy); if (!sptr || !dptr) { vm_log(vm,"DMA","unable to transfer from 0x%llx to 0x%llx\n",src,dst); return; } sl = VM_PAGE_SIZE - (src & VM_PAGE_IMASK); dl = VM_PAGE_SIZE - (dst & VM_PAGE_IMASK); clen = m_min(sl,dl); clen = m_min(clen,len); memcpy(dptr,sptr,clen); src += clen; dst += clen; len -= clen; } }
/* Read a packet from the local FIFO queue */ static ssize_t netio_fifo_recv(netio_fifo_desc_t *nfd,void *pkt,size_t max_len) { struct timespec ts; m_tmcnt_t expire; netio_fifo_pkt_t *p; size_t len = -1; /* Wait for the endpoint to signal a new arriving packet */ expire = m_gettime_usec() + 50000; ts.tv_sec = expire / 1000000; ts.tv_nsec = (expire % 1000000) * 1000; pthread_mutex_lock(&nfd->lock); pthread_cond_timedwait(&nfd->cond,&nfd->lock,&ts); /* Extract a packet from the list */ p = netio_fifo_extract_pkt(nfd); pthread_mutex_unlock(&nfd->lock); if (p) { len = m_min(p->pkt_len,max_len); memcpy(pkt,p->pkt,len); free(p); } return(len); }
/* MapPreviewCanvas::showMap * Adjusts zoom and offset to show the whole map *******************************************************************/ void MapPreviewCanvas::showMap() { // Find extents of map mep_vertex_t m_min(999999.0, 999999.0); mep_vertex_t m_max(-999999.0, -999999.0); for (unsigned a = 0; a < verts.size(); a++) { if (verts[a].x < m_min.x) m_min.x = verts[a].x; if (verts[a].x > m_max.x) m_max.x = verts[a].x; if (verts[a].y < m_min.y) m_min.y = verts[a].y; if (verts[a].y > m_max.y) m_max.y = verts[a].y; } // Offset to center of map double width = m_max.x - m_min.x; double height = m_max.y - m_min.y; offset_x = m_min.x + (width * 0.5); offset_y = m_min.y + (height * 0.5); // Zoom to fit whole map double x_scale = ((double)GetClientSize().x) / width; double y_scale = ((double)GetClientSize().y) / height; zoom = MIN(x_scale, y_scale); zoom *= 0.95; }
// ----------------------------------------------------------------------------- // Adjusts zoom and offset to show the whole map // ----------------------------------------------------------------------------- void MapPreviewCanvas::showMap() { // Find extents of map Vertex m_min(999999.0, 999999.0); Vertex m_max(-999999.0, -999999.0); for (auto& vert : verts_) { if (vert.x < m_min.x) m_min.x = vert.x; if (vert.x > m_max.x) m_max.x = vert.x; if (vert.y < m_min.y) m_min.y = vert.y; if (vert.y > m_max.y) m_max.y = vert.y; } // Offset to center of map double width = m_max.x - m_min.x; double height = m_max.y - m_min.y; offset_ = { m_min.x + (width * 0.5), m_min.y + (height * 0.5) }; // Zoom to fit whole map double x_scale = ((double)GetClientSize().x) / width; double y_scale = ((double)GetClientSize().y) / height; zoom_ = std::min<double>(x_scale, y_scale); zoom_ *= 0.95; }
CDataContainer CIntTypeInfo::serialize() const { CDataContainer serializedData; if (m_min.isDefined()) serializedData.set("min", m_min()); if (m_max.isDefined()) serializedData.set("max", m_max()); if (m_step.isDefined()) serializedData.set("step", m_step()); return serializedData; }
double ighmm_rand_normal_right (double a, double mue, double u, int seed) { # define CUR_PROC "ighmm_rand_normal_right" double x = -1; double sigma; #ifdef DO_WITH_GSL double s; #else double U, Us, Us1, Feps, t, T; #endif if (u <= 0.0) { GHMM_LOG(LCONVERTED, "u <= 0.0 not allowed\n"); goto STOP; } sigma = sqrt(u); if (seed != 0) { GHMM_RNG_SET (RNG, seed); } #ifdef DO_WITH_GSL /* move boundary to lower values in order to achieve maximum at mue gsl_ran_gaussian_tail(generator, lower_boundary, sigma) */ return mue + gsl_ran_gaussian_tail(RNG, a - mue, sqrt (u)); #else /* DO_WITH_GSL */ /* Inverse transformation with restricted sampling by Fishman */ U = GHMM_RNG_UNIFORM(RNG); Feps = ighmm_rand_get_PHI((a-mue) / sigma); Us = Feps + (1-Feps) * U; Us1 = 1-Us; t = m_min (Us, Us1); t = sqrt (-log (t * t)); T = sigma * (t - (C0 + t * (C1 + t * C2)) / (1 + t * (D1 + t * (D2 + t * D3)))); if (Us < Us1) x = mue - T; else x = mue + T; #endif /* DO_WITH_GSL */ STOP: return x; # undef CUR_PROC } /* randvar_normal_pos */
//! Get stabilized magnetic field. //! @param[in] msg magnetic field message. void updateField(const IMC::MagneticField& msg) { // Insert magnetic field into row matrix. Math::Matrix mf(1,3); mf(0) = msg.x; mf(1) = msg.y; mf(2) = msg.z; // Get stabilized magnetic field. Math::Matrix mf_stab = mf * transpose(m_dcm.toDCM()); // Store maximum and minimum values. for (unsigned i = 0; i < 3; i++) { if (mf_stab(i) > m_max(i)) m_max(i) = mf_stab(i); if (mf_stab(i) < m_min(i)) m_min(i) = mf_stab(i); } }
/* 現在の Depth, Field... を評価する。 */ static uint Real_GetEval(void) // ret: この局面の評価, 0 は返さない。 { uint evals[FIELD_W_MAX]; uint ret; uint index; if(MaxDepth <= Depth) // ? 探索の上限に達した。-> これ以上先の手は読まない。 return GetCurrEval(); // 現局面で決着 -> これ以上先の手は読まない。 { uint winner = GetWinner(); if(winner == 1) return 400000000 + 100 - Depth; // 早い勝利を高く評価 if(winner == 2) return 1; } GetNextEvals(evals); for(index = 0; index < Field_W; index++) if(evals[index]) break; if(index == Field_W) // 次の手が無い -> 引き分け { ret = 300000000; } else if(Depth % 2) // 相手の手番 -> (自分にとって)最悪手を選ぶ { ret = UINTMAX; for(index = 0; index < Field_W; index++) if(evals[index]) ret = m_min(ret, evals[index]); } else // 自分の手番 -> 最善手を選ぶ { ret = 0; for(index = 0; index < Field_W; index++) if(evals[index]) ret = m_max(ret, evals[index]); } if(ret / 100000000 == 2) // ? 未決着コース if(IsTaboo()) ret -= 100000000; return ret; }
/* Receive an ethernet frame */ ssize_t gen_eth_recv(pcap_t *p,char *buffer,size_t len) { struct pcap_pkthdr pkt_info; u_char *pkt_ptr; ssize_t rlen; if (!(pkt_ptr = (u_char *)pcap_next(p,&pkt_info))) return(-1); rlen = m_min(len,pkt_info.caplen); memcpy(buffer,pkt_ptr,rlen); return(rlen); }
void VTreeToStream(VTree_t *vt, void (*streamWriter)(uchar *, uint)) { uint count = vt->GetLocalCount(); uint index; uchar buffer[9]; for(index = 0; index < count; index++) { char *file = vt->GetLocal(index); streamWriter(file, strlen(file) + 1); if(vt->IsDir(index)) { buffer[0] = SIGN_DIR; streamWriter(buffer, 1); vt->IntoDir(index); VTreeToStream(vt, streamWriter); vt->ExitDir(); } else { uint64 size = vt->GetSize(index); uint64 readPos; uint readSize; void *block = memAlloc(WRITER_BUFFSIZE); buffer[0] = SIGN_FILE; value64ToBlock(buffer + 1, size); streamWriter(buffer, 9); for(readPos = 0; readPos < size; readPos += readSize) { readSize = m_min(WRITER_BUFFSIZE, (uint)(size - readPos)); vt->GetEntity(index, readPos, readSize, block); streamWriter(block, readSize); } memFree(block); } memFree(file); } buffer[0] = SIGN_ENDDIR; streamWriter(buffer, 1); }
/* Put a packet in buffer of a descriptor */ static void mv64460_sdma_rxdesc_put_pkt(struct mv64460_data *d, struct sdma_desc *rxd, u_char **pkt,ssize_t *pkt_len) { ssize_t len,cp_len; len = (rxd->buf_size & MV64460_RXDESC_BS_MASK) >> MV64460_RXDESC_BS_SHIFT; /* compute the data length to copy */ cp_len = m_min(len,*pkt_len); /* copy packet data to the VM physical RAM */ physmem_copy_to_vm(d->vm,*pkt,rxd->buf_ptr,cp_len); /* set the byte count in descriptor */ rxd->buf_size |= cp_len; *pkt += cp_len; *pkt_len -= cp_len; }
/* Packet handler: write packets to a file in CAP format */ static int pf_capture_pkt_handler(netio_desc_t *nio,void *pkt,size_t len, void *opt) { struct netio_filter_capture *c = opt; struct pcap_pkthdr pkt_hdr; if (c != NULL) { gettimeofday(&pkt_hdr.ts,0); pkt_hdr.caplen = m_min(len, (u_int)pcap_snapshot(c->desc)); pkt_hdr.len = len; /* thread safe dump */ pthread_mutex_lock(&c->lock); pcap_dump((u_char *)c->dumper,&pkt_hdr,pkt); pcap_dump_flush(c->dumper); pthread_mutex_unlock(&c->lock); } return(NETIO_FILTER_ACTION_PASS); }
/* Put a packet in buffer of a descriptor */ static ssize_t rxdesc_put_pkt(struct pos_oc3_data *d,struct rx_desc *rxd, u_char **pkt,ssize_t *pkt_len) { ssize_t len,cp_len; len = rxd->rdes[0] & POS_OC3_RXDESC_LEN_MASK; /* compute the data length to copy */ cp_len = m_min(len,*pkt_len); #if DEBUG_RECEIVE POS_LOG(d,"copying %d bytes at 0x%x\n",cp_len,rxd->rdes[1]); #endif /* copy packet data to the VM physical RAM */ physmem_copy_to_vm(d->vm,*pkt,rxd->rdes[1],cp_len); *pkt += cp_len; *pkt_len -= cp_len; return(cp_len); }
static ssize_t nio_ethernet_recv(nio_ethernet_t *nio_ethernet, void *pkt, size_t max_len) { struct pcap_pkthdr *pkt_info; const u_char *pkt_data; ssize_t rlen; int res; timedout: res = pcap_next_ex(nio_ethernet->pcap_dev, &pkt_info, &pkt_data); if (res == 0) { /* Timeout elapsed */ goto timedout; } if(res == -1) { fprintf(stderr, "pcap_next_ex: %s\n", pcap_geterr(nio_ethernet->pcap_dev)); return (-1); } rlen = m_min(max_len, pkt_info->caplen); memcpy(pkt, pkt_data, rlen); return (rlen); }
/* Load an ELF image into the simulated memory. Using libelf*/ int mips_load_elf_image (cpu_mips_t * cpu, char *filename, m_va_t * entry_point) { m_va_t vaddr; m_uint32_t remain; void *haddr; Elf32_Ehdr *ehdr; Elf32_Shdr *shdr; Elf_Scn *scn; Elf *img_elf; size_t len, clen; int i, fd; FILE *bfd; if (! filename) return (-1); #ifdef __CYGWIN__ fd = open (filename, O_RDONLY | O_BINARY); #else fd = open (filename, O_RDONLY); #endif printf ("Loading ELF file '%s'...\n", filename); if (fd == -1) { perror ("load_elf_image: open"); return (-1); } if (elf_version (EV_CURRENT) == EV_NONE) { fprintf (stderr, "load_elf_image: library out of date\n"); return (-1); } if (!(img_elf = elf_begin (fd, ELF_C_READ, NULL))) { fprintf (stderr, "load_elf_image: elf_begin: %s\n", elf_errmsg (elf_errno ())); return (-1); } if (!(ehdr = elf32_getehdr (img_elf))) { fprintf (stderr, "load_elf_image: invalid ELF file\n"); return (-1); } bfd = fdopen (fd, "rb"); if (!bfd) { perror ("load_elf_image: fdopen"); return (-1); } // if (!skip_load) { for (i = 0; i < ehdr->e_shnum; i++) { scn = elf_getscn (img_elf, i); shdr = elf32_getshdr (scn); len = shdr->sh_size; if (!(shdr->sh_flags & SHF_ALLOC) || !len) continue; fseek (bfd, shdr->sh_offset, SEEK_SET); vaddr = sign_extend (shdr->sh_addr, 32); if (cpu->vm->debug_level > 0) { printf (" * Adding section at virtual address 0x%8.8" LL "x " "(len=0x%8.8lx)\n", vaddr & 0xFFFFFFFF, (u_long) len); } while (len > 0) { haddr = cpu->mem_op_lookup (cpu, vaddr); if (!haddr) { fprintf (stderr, "load_elf_image: invalid load address 0x%" LL "x\n", vaddr); return (-1); } if (len > MIPS_MIN_PAGE_SIZE) clen = MIPS_MIN_PAGE_SIZE; else clen = len; remain = MIPS_MIN_PAGE_SIZE; remain -= (vaddr - (vaddr & MIPS_MIN_PAGE_SIZE)); clen = m_min (clen, remain); if (fread ((u_char *) haddr, clen, 1, bfd) < 1) break; vaddr += clen; len -= clen; } } printf ("ELF entry point: 0x%x\n", ehdr->e_entry); if (entry_point) *entry_point = ehdr->e_entry; elf_end (img_elf); fclose (bfd); return (0); }
// Main int main(int argc, char ** argv) { // Choose the best GPU in case there are multiple available choose_GPU(); // Keep track of the start time of the program long long program_start_time = get_time(); if (argc !=3){ fprintf(stderr, "usage: %s <input file> <number of frames to process>", argv[0]); exit(1); } // Let the user specify the number of frames to process int num_frames = atoi(argv[2]); // Open video file char *video_file_name = argv[1]; avi_t *cell_file = AVI_open_input_file(video_file_name, 1); if (cell_file == NULL) { AVI_print_error("Error with AVI_open_input_file"); return -1; } int i, j, *crow, *ccol, pair_counter = 0, x_result_len = 0, Iter = 20, ns = 4, k_count = 0, n; MAT *cellx, *celly, *A; double *GICOV_spots, *t, *G, *x_result, *y_result, *V, *QAX_CENTERS, *QAY_CENTERS; double threshold = 1.8, radius = 10.0, delta = 3.0, dt = 0.01, b = 5.0; // Extract a cropped version of the first frame from the video file MAT *image_chopped = get_frame(cell_file, 0, 1, 0); printf("Detecting cells in frame 0\n"); // Get gradient matrices in x and y directions MAT *grad_x = gradient_x(image_chopped); MAT *grad_y = gradient_y(image_chopped); // Allocate for gicov_mem and strel gicov_mem = (float*) malloc(sizeof(float) * grad_x->m * grad_y->n); strel = (float*) malloc(sizeof(float) * strel_m * strel_n); m_free(image_chopped); int grad_m = grad_x->m; int grad_n = grad_y->n; #pragma acc data create(sin_angle,cos_angle,theta,tX,tY) \ create(gicov_mem[0:grad_x->m*grad_y->n]) { // Precomputed constants on GPU compute_constants(); // Get GICOV matrices corresponding to image gradients long long GICOV_start_time = get_time(); MAT *gicov = GICOV(grad_x, grad_y); long long GICOV_end_time = get_time(); // Dilate the GICOV matrices long long dilate_start_time = get_time(); MAT *img_dilated = dilate(gicov); long long dilate_end_time = get_time(); } /* end acc data */ // Find possible matches for cell centers based on GICOV and record the rows/columns in which they are found pair_counter = 0; crow = (int *) malloc(gicov->m * gicov->n * sizeof(int)); ccol = (int *) malloc(gicov->m * gicov->n * sizeof(int)); for(i = 0; i < gicov->m; i++) { for(j = 0; j < gicov->n; j++) { if(!double_eq(m_get_val(gicov,i,j), 0.0) && double_eq(m_get_val(img_dilated,i,j), m_get_val(gicov,i,j))) { crow[pair_counter]=i; ccol[pair_counter]=j; pair_counter++; } } } GICOV_spots = (double *) malloc(sizeof(double) * pair_counter); for(i = 0; i < pair_counter; i++) GICOV_spots[i] = sqrt(m_get_val(gicov, crow[i], ccol[i])); G = (double *) calloc(pair_counter, sizeof(double)); x_result = (double *) calloc(pair_counter, sizeof(double)); y_result = (double *) calloc(pair_counter, sizeof(double)); x_result_len = 0; for (i = 0; i < pair_counter; i++) { if ((crow[i] > 29) && (crow[i] < BOTTOM - TOP + 39)) { x_result[x_result_len] = ccol[i]; y_result[x_result_len] = crow[i] - 40; G[x_result_len] = GICOV_spots[i]; x_result_len++; } } // Make an array t which holds each "time step" for the possible cells t = (double *) malloc(sizeof(double) * 36); for (i = 0; i < 36; i++) { t[i] = (double)i * 2.0 * PI / 36.0; } // Store cell boundaries (as simple circles) for all cells cellx = m_get(x_result_len, 36); celly = m_get(x_result_len, 36); for(i = 0; i < x_result_len; i++) { for(j = 0; j < 36; j++) { m_set_val(cellx, i, j, x_result[i] + radius * cos(t[j])); m_set_val(celly, i, j, y_result[i] + radius * sin(t[j])); } } A = TMatrix(9,4); V = (double *) malloc(sizeof(double) * pair_counter); QAX_CENTERS = (double * )malloc(sizeof(double) * pair_counter); QAY_CENTERS = (double *) malloc(sizeof(double) * pair_counter); memset(V, 0, sizeof(double) * pair_counter); memset(QAX_CENTERS, 0, sizeof(double) * pair_counter); memset(QAY_CENTERS, 0, sizeof(double) * pair_counter); // For all possible results, find the ones that are feasibly leukocytes and store their centers k_count = 0; for (n = 0; n < x_result_len; n++) { if ((G[n] < -1 * threshold) || G[n] > threshold) { MAT * x, *y; VEC * x_row, * y_row; x = m_get(1, 36); y = m_get(1, 36); x_row = v_get(36); y_row = v_get(36); // Get current values of possible cells from cellx/celly matrices x_row = get_row(cellx, n, x_row); y_row = get_row(celly, n, y_row); uniformseg(x_row, y_row, x, y); // Make sure that the possible leukocytes are not too close to the edge of the frame if ((m_min(x) > b) && (m_min(y) > b) && (m_max(x) < cell_file->width - b) && (m_max(y) < cell_file->height - b)) { MAT * Cx, * Cy, *Cy_temp, * Ix1, * Iy1; VEC *Xs, *Ys, *W, *Nx, *Ny, *X, *Y; Cx = m_get(1, 36); Cy = m_get(1, 36); Cx = mmtr_mlt(A, x, Cx); Cy = mmtr_mlt(A, y, Cy); Cy_temp = m_get(Cy->m, Cy->n); for (i = 0; i < 9; i++) m_set_val(Cy, i, 0, m_get_val(Cy, i, 0) + 40.0); // Iteratively refine the snake/spline for (i = 0; i < Iter; i++) { int typeofcell; if(G[n] > 0.0) typeofcell = 0; else typeofcell = 1; splineenergyform01(Cx, Cy, grad_x, grad_y, ns, delta, 2.0 * dt, typeofcell); } X = getsampling(Cx, ns); for (i = 0; i < Cy->m; i++) m_set_val(Cy_temp, i, 0, m_get_val(Cy, i, 0) - 40.0); Y = getsampling(Cy_temp, ns); Ix1 = linear_interp2(grad_x, X, Y); Iy1 = linear_interp2(grad_x, X, Y); Xs = getfdriv(Cx, ns); Ys = getfdriv(Cy, ns); Nx = v_get(Ys->dim); for (i = 0; i < Ys->dim; i++) v_set_val(Nx, i, v_get_val(Ys, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i))); Ny = v_get(Xs->dim); for (i = 0; i < Xs->dim; i++) v_set_val(Ny, i, -1.0 * v_get_val(Xs, i) / sqrt(v_get_val(Xs, i)*v_get_val(Xs, i) + v_get_val(Ys, i)*v_get_val(Ys, i))); W = v_get(Nx->dim); for (i = 0; i < Nx->dim; i++) v_set_val(W, i, m_get_val(Ix1, 0, i) * v_get_val(Nx, i) + m_get_val(Iy1, 0, i) * v_get_val(Ny, i)); V[n] = mean(W) / std_dev(W); // Find the cell centers by computing the means of X and Y values for all snaxels of the spline contour QAX_CENTERS[k_count] = mean(X); QAY_CENTERS[k_count] = mean(Y) + TOP; k_count++; // Free memory v_free(W); v_free(Ny); v_free(Nx); v_free(Ys); v_free(Xs); m_free(Iy1); m_free(Ix1); v_free(Y); v_free(X); m_free(Cy_temp); m_free(Cy); m_free(Cx); } // Free memory v_free(y_row); v_free(x_row); m_free(y); m_free(x); } } // Free memory free(gicov_mem); free(strel); free(V); free(ccol); free(crow); free(GICOV_spots); free(t); free(G); free(x_result); free(y_result); m_free(A); m_free(celly); m_free(cellx); m_free(img_dilated); m_free(gicov); m_free(grad_y); m_free(grad_x); // Report the total number of cells detected printf("Cells detected: %d\n\n", k_count); // Report the breakdown of the detection runtime printf("Detection runtime\n"); printf("-----------------\n"); printf("GICOV computation: %.5f seconds\n", ((float) (GICOV_end_time - GICOV_start_time)) / (1000*1000)); printf(" GICOV dilation: %.5f seconds\n", ((float) (dilate_end_time - dilate_start_time)) / (1000*1000)); printf(" Total: %.5f seconds\n", ((float) (get_time() - program_start_time)) / (1000*1000)); // Now that the cells have been detected in the first frame, // track the ellipses through subsequent frames if (num_frames > 1) printf("\nTracking cells across %d frames\n", num_frames); else printf("\nTracking cells across 1 frame\n"); long long tracking_start_time = get_time(); int num_snaxels = 20; ellipsetrack(cell_file, QAX_CENTERS, QAY_CENTERS, k_count, radius, num_snaxels, num_frames); printf(" Total: %.5f seconds\n", ((float) (get_time() - tracking_start_time)) / (float) (1000*1000*num_frames)); // Report total program execution time printf("\nTotal application run time: %.5f seconds\n", ((float) (get_time() - program_start_time)) / (1000*1000)); return 0; }
/* Put a packet into SDMA buffers */ static int mv64460_sdma_handle_rxqueue(struct mv64460_data *d, struct sdma_channel *channel, u_char *pkt,ssize_t pkt_len) { m_uint32_t rx_start,rx_current; struct sdma_desc rxd0,rxdn,*rxdc; ssize_t tot_len = pkt_len; u_char *pkt_ptr = pkt; int i; /* Truncate the packet if it is too big */ pkt_len = m_min(pkt_len,MV64460_MAX_PKT_SIZE); /* Copy the first RX descriptor */ if (!(rx_start = rx_current = channel->scrdp)) goto dma_error; /* Load the first RX descriptor */ mv64460_sdma_desc_read(d,rx_start,&rxd0); #if DEBUG_SDMA MV64460_LOG(d,"SDMA channel %u: reading desc at 0x%8.8x " "[buf_size=0x%8.8x,cmd_stat=0x%8.8x," "next_ptr=0x%8.8x,buf_ptr=0x%8.8x]\n", channel->id,rx_start,rxd0.buf_size,rxd0.cmd_stat, rxd0.next_ptr,rxd0.buf_ptr); #endif for(i=0,rxdc=&rxd0;tot_len>0;i++) { /* We must own the descriptor */ if (!(rxdc->cmd_stat & MV64460_RXDESC_OWN)) goto dma_error; /* Put data into the descriptor buffer */ mv64460_sdma_rxdesc_put_pkt(d,rxdc,&pkt_ptr,&tot_len); /* Clear the OWN bit */ rxdc->cmd_stat &= ~MV64460_RXDESC_OWN; /* We have finished if the complete packet has been stored */ if (tot_len == 0) { rxdc->cmd_stat |= MV64460_RXDESC_L; /* Fake HDLC CRC */ if (mv64460_mpsc_get_channel_mode(d,channel->id) == MV64460_MPSC_MODE_HDLC) { rxdc->buf_size += 2; /* Add 2 bytes for CRC */ } } /* Update the descriptor in host memory (but not the 1st) */ if (i != 0) mv64460_sdma_desc_write(d,rx_current,rxdc); /* Get address of the next descriptor */ rx_current = rxdc->next_ptr; if (tot_len == 0) break; if (!rx_current) goto dma_error; /* Read the next descriptor from VM physical RAM */ mv64460_sdma_desc_read(d,rx_current,&rxdn); rxdc = &rxdn; } /* Update the RX pointers */ channel->scrdp = rx_current; /* Update the first RX descriptor */ rxd0.cmd_stat |= MV64460_RXDESC_F; mv64460_sdma_desc_write(d,rx_start,&rxd0); /* Indicate that we have a frame ready */ mv64460_sdma_set_cause(d,channel->id,MV64460_SDMA_CAUSE_RXBUF0); mv64460_sdma_update_int_status(d); return(TRUE); dma_error: mv64460_sdma_set_cause(d,channel->id,MV64460_SDMA_CAUSE_RXERR0); mv64460_sdma_update_int_status(d); return(FALSE); }
/* MapPreviewCanvas::createImage * Draws the map in an image * TODO: Factorize code with normal draw() and showMap() functions. * TODO: Find a way to generate an arbitrary-sized image through * tiled rendering. *******************************************************************/ void MapPreviewCanvas::createImage(ArchiveEntry& ae, int width, int height) { // Find extents of map mep_vertex_t m_min(999999.0, 999999.0); mep_vertex_t m_max(-999999.0, -999999.0); for (unsigned a = 0; a < verts.size(); a++) { if (verts[a].x < m_min.x) m_min.x = verts[a].x; if (verts[a].x > m_max.x) m_max.x = verts[a].x; if (verts[a].y < m_min.y) m_min.y = verts[a].y; if (verts[a].y > m_max.y) m_max.y = verts[a].y; } double mapwidth = m_max.x - m_min.x; double mapheight = m_max.y - m_min.y; if (width == 0) width = -5; if (height == 0) height = -5; if (width < 0) width = mapwidth / abs(width); if (height < 0) height = mapheight / abs(height); // Setup colours rgba_t col_save_background = ColourConfiguration::getColour("map_image_background"); rgba_t col_save_line_1s = ColourConfiguration::getColour("map_image_line_1s"); rgba_t col_save_line_2s = ColourConfiguration::getColour("map_image_line_2s"); rgba_t col_save_line_special = ColourConfiguration::getColour("map_image_line_special"); rgba_t col_save_line_macro = ColourConfiguration::getColour("map_image_line_macro"); // Setup OpenGL rigmarole GLuint texID, fboID; if (GLEW_ARB_framebuffer_object) { glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); // We don't use mipmaps, but OpenGL will refuse to attach // the texture to the framebuffer if they are not present glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindTexture(GL_TEXTURE_2D, 0); glGenFramebuffersEXT(1, &fboID); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texID, 0); GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); } glViewport(0, 0, width, height); // Setup the screen projection glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, width, 0, height, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Clear glClearColor(((double)col_save_background.r)/255.f, ((double)col_save_background.g)/255.f, ((double)col_save_background.b)/255.f, ((double)col_save_background.a)/255.f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // Translate to inside of pixel (otherwise inaccuracies can occur on certain gl implementations) if (OpenGL::accuracyTweak()) glTranslatef(0.375f, 0.375f, 0); // Zoom/offset to show full map // Offset to center of map offset_x = m_min.x + (mapwidth * 0.5); offset_y = m_min.y + (mapheight * 0.5); // Zoom to fit whole map double x_scale = ((double)width) / mapwidth; double y_scale = ((double)height) / mapheight; zoom = MIN(x_scale, y_scale); zoom *= 0.95; // Translate to middle of canvas glTranslated(width>>1, height>>1, 0); // Zoom glScaled(zoom, zoom, 1); // Translate to offset glTranslated(-offset_x, -offset_y, 0); // Setup drawing glDisable(GL_TEXTURE_2D); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glLineWidth(map_image_thickness); glEnable(GL_LINE_SMOOTH); // Draw lines for (unsigned a = 0; a < lines.size(); a++) { mep_line_t line = lines[a]; // Check ends if (line.v1 >= verts.size() || line.v2 >= verts.size()) continue; // Get vertices mep_vertex_t v1 = verts[lines[a].v1]; mep_vertex_t v2 = verts[lines[a].v2]; // Set colour if (line.special) OpenGL::setColour(col_save_line_special); else if (line.macro) OpenGL::setColour(col_save_line_macro); else if (line.twosided) OpenGL::setColour(col_save_line_2s); else OpenGL::setColour(col_save_line_1s); // Draw line glBegin(GL_LINES); glVertex2d(v1.x, v1.y); glVertex2d(v2.x, v2.y); glEnd(); } glLineWidth(1.0f); glDisable(GL_LINE_SMOOTH); uint8_t* ImageBuffer = new uint8_t[width * height * 4]; glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, ImageBuffer); if (GLEW_ARB_framebuffer_object) { glBindFramebuffer(GL_FRAMEBUFFER, 0); glDeleteTextures( 1, &texID ); glDeleteFramebuffersEXT( 1, &fboID ); } SImage img; img.setImageData(ImageBuffer, width, height, RGBA); img.mirror(true); MemChunk mc; SIFormat::getFormat("png")->saveImage(img, mc); ae.importMemChunk(mc); }
double randvar_normal_pos (double mue, double u, int seed) { # define CUR_PROC "randvar_normal_pos" double x = -1; double sigma; #ifdef DO_WITH_GSL double s; #else double U, Us, Us1, Feps, Feps1, t, T; #endif if (u <= 0.0) { mes_prot ("u <= 0.0 not allowed\n"); goto STOP; } sigma = sqrt (u); if (seed != 0) { GHMM_RNG_SET (RNG, seed); return (1.0); } #ifdef DO_WITH_GSL /* up to version 0.8 gsl_ran_gaussian_tail can not handle negative cutoff */ #define GSL_RAN_GAUSSIAN_TAIL_BUG 1 #ifdef GSL_RAN_GAUSSIAN_TAIL_BUG s = (-mue) / sigma; if (s < 1) { do { x = gsl_ran_gaussian (RNG, 1.0); } while (x < s); return x * sigma + mue; } #endif /* GSL_RAN_GAUSSIAN_TAIL_BUG */ /* move boundary to lower values in order to achieve maximum at mue gsl_ran_gaussian_tail(generator, lower_boundary, sigma) */ return gsl_ran_gaussian_tail (RNG, -mue, sqrt (u)) + mue; #else /* DO_WITH_GSL */ /* Method: Generate Gauss-distributed random nunbers (with GSL-lib.), until a positive one is found -> not very effective if mue << 0 while (x < 0.0) { x = sigma * randvar_std_normal(seed) + mue; } */ /* Inverse transformation with restricted sampling by Fishman */ U = GHMM_RNG_UNIFORM (RNG); Feps = randvar_get_PHI (-(EPS_NDT + mue) / sigma); Us = Feps + (1 - Feps) * U; /* Numerically better: 1-Us = 1-Feps - (1-Feps)*U, therefore: Feps1 = 1-Feps, Us1 = 1-Us */ Feps1 = randvar_get_PHI ((EPS_NDT + mue) / sigma); Us1 = Feps1 - Feps1 * U; t = m_min (Us, Us1); t = sqrt (-log (t * t)); T = sigma * (t - (C0 + t * (C1 + t * C2)) / (1 + t * (D1 + t * (D2 + t * D3)))); if (Us - 0.5 < 0) x = mue - T; else x = mue + T; #endif /* DO_WITH_GSL */ STOP: return (x); # undef CUR_PROC } /* randvar_normal_pos */
/* * Put a packet in the RX ring. */ static void dev_pos_oc3_receive_pkt(struct pos_oc3_data *d, u_char *pkt,ssize_t pkt_len) { m_uint32_t rx_start,rxdn_addr,rxdn_rdes0; struct rx_desc rxd0,rxdn,*rxdc; ssize_t cp_len,tot_len = pkt_len; u_char *pkt_ptr = pkt; int i; if (d->rx_start == 0) return; /* Truncate the packet if it is too big */ pkt_len = m_min(pkt_len,POS_OC3_MAX_PKT_SIZE); /* Copy the current rxring descriptor */ rxdesc_read(d,d->rx_current,&rxd0); /* We must have the first descriptor... */ if (!rxdesc_acquire(rxd0.rdes[0])) return; /* Remember the first RX descriptor address */ rx_start = d->rx_current; for(i=0,rxdc=&rxd0;tot_len>0;i++) { /* Put data into the descriptor buffers */ cp_len = rxdesc_put_pkt(d,rxdc,&pkt_ptr,&tot_len); /* Get address of the next descriptor */ rxdn_addr = rxdesc_get_next(d,d->rx_current,rxdc); /* We have finished if the complete packet has been stored */ if (tot_len == 0) { rxdc->rdes[0] = (cp_len + d->crc_size); if (i != 0) physmem_copy_u32_to_vm(d->vm,d->rx_current,rxdc->rdes[0]); d->rx_current = rxdn_addr; break; } #if DEBUG_RECEIVE POS_LOG(d,"trying to acquire new descriptor at 0x%x\n",rxdn_addr); #endif /* Get status of the next descriptor to see if we can acquire it */ rxdn_rdes0 = physmem_copy_u32_from_vm(d->vm,rxdn_addr); if (!rxdesc_acquire(rxdn_rdes0)) rxdc->rdes[0] = 0; /* error, no buf available (special flag?) */ else rxdc->rdes[0] = POS_OC3_RXDESC_CONT; /* packet continues */ rxdc->rdes[0] |= cp_len; /* Update the new status (only if we are not on the first desc) */ if (i != 0) physmem_copy_u32_to_vm(d->vm,d->rx_current,rxdc->rdes[0]); /* Update the RX pointer */ d->rx_current = rxdn_addr; if (!(rxdc->rdes[0] & POS_OC3_RXDESC_CONT)) break; /* Read the next descriptor from VM physical RAM */ rxdesc_read(d,rxdn_addr,&rxdn); rxdc = &rxdn; } /* Update the first RX descriptor */ physmem_copy_u32_to_vm(d->vm,rx_start,rxd0.rdes[0]); /* Generate IRQ on CPU */ pci_dev_trigger_irq(d->vm,d->pci_dev); }
/*============================================================================*/ static int ighmm_hlist_prop_forward (ghmm_dmodel * mo, hypoList * h, hypoList ** hplus, int labels, int *nr_s, int *max_out) { #define CUR_PROC "ighmm_hlist_prop_forward" int i, j, c, k; int i_id, j_id, g_nr; int no_oldHyps = 0, newHyps = 0; hypoList *hP = h; hypoList **created; ARRAY_MALLOC (created, labels); /* extend the all hypotheses with the labels of out_states of all states in the hypotesis */ while (hP != NULL) { /* lookup table for labels, created[i]!=0 iff the current hypotheses was propagated forward with label i */ for (c = 0; c < labels; c++) created[c] = NULL; /* extend the current hypothesis and add all states which may have probability greater null */ for (i = 0; i < hP->gamma_states; i++) { /* skip impossible states */ if (hP->gamma_a[i] == 1.0) continue; i_id = hP->gamma_id[i]; for (j = 0; j < mo->s[i_id].out_states; j++) { j_id = mo->s[i_id].out_id[j]; c = mo->label[j_id]; /* create a new hypothesis with label c */ if (!created[c]) { ighmm_hlist_insert (hplus, c, hP); created[c] = *hplus; /* initiallize gamma-array with safe size (number of states */ ARRAY_MALLOC ((*hplus)->gamma_id, m_min (nr_s[c], hP->gamma_states * max_out[hP->hyp_c])); (*hplus)->gamma_id[0] = j_id; (*hplus)->gamma_states = 1; newHyps++; } /* add a new gamma state to the existing hypothesis with c */ else { g_nr = created[c]->gamma_states; /* search for state j_id in the gamma list */ for (k = 0; k < g_nr; k++) if (j_id == created[c]->gamma_id[k]) break; /* add the state to the gamma list */ if (k == g_nr) { created[c]->gamma_id[g_nr] = j_id; created[c]->gamma_states = g_nr + 1; } } } } /* reallocating gamma-array to the correct size */ for (c = 0; c < labels; c++) { if (created[c]) { ARRAY_CALLOC (created[c]->gamma_a, created[c]->gamma_states); ARRAY_REALLOC (created[c]->gamma_id, created[c]->gamma_states); created[c] = NULL; } } hP = hP->next; no_oldHyps++; } /* printf("Created %d new Hypotheses.\n", newHyps); */ free (created); return (no_oldHyps); STOP: /* Label STOP from ARRAY_[CM]ALLOC */ GHMM_LOG(LCONVERTED, "ighmm_hlist_prop_forward failed\n"); exit (1); #undef CUR_PROC }