/******************************************************************** * サンプリング・コマンド ******************************************************************** HIDASP_SAMPLING(param,count) ---> return header[16] + packet[count]; ------------- a/d | a | rate | count | ch(s) | trig-mode | trig-ch | port-sel | rep | d | rate | count | ch(s) | trig-mode | trig-ch | port-sel | rep | */ void cmd_sampling(void) { int size = BSWAP16(PacketFromPC.size); sample_init(&PacketFromPC); sample_read(sampling_buf,size); USBwrite(sampling_buf,size); }
void sample_init_read(sample_data *s, FILE *fp) { int *bond_site, i; double *bond_value; bond_site = malloc(NZ * sizeof(int)); bond_value = malloc(NUM_BONDS * sizeof(double)); for (i = 0; i < NUM_BONDS; i++) { int c, s0, s1; c = fscanf(fp, "%d %d %lf", &s0, &s1, &(bond_value[i])); assert(c != EOF); assert(IS_SITE_INDEX(s0) && IS_SITE_INDEX(s1)); bond_site[2*i] = s0; bond_site[2*i+1] = s1; } sample_init(s, bond_site, bond_value); free(bond_site); free(bond_value); }
int __cdecl main(int argc, char *argv[]) { char name[32]; int fd, i, data, ret; UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); ret = sample_init(); if (ret) { fprintf(stderr, "ERROR: Failed to initialize module\n"); exit(1); } for (i = 0;; i++) { snprintf(name, sizeof(name), "sample%d", i); fd = linuxemu_dev_open(name); if (fd < 0) break; printf("%s opened\n", name); dump_and_read(fd); data = MAKE_DATA(i); if (linuxemu_dev_ioctl(fd, IOCTL_SAMPLE_SET, &data)) { printf("ioctl failed(IOCTL_SAMPLE_SET)\n"); } dump_and_read(fd); linuxemu_dev_close(fd); } sample_exit(); return 0; }
/* * Find smallest lambda1 that makes all coefficients * zero (except the intercept) * */ double get_lambda1max_gmatrix(gmatrix *g, phi1 phi1_func, phi2 phi2_func, inv inv_func, step step_func) { int i, j, n = g->ncurr, n1 = g->ncurr - 1; long p1 = g->p + 1; int k, K = g->K; double d1, d2, s, zmax = 0, beta_new; long jp1k; sample sm; if(!sample_init(&sm)) return FAILURE; for(k = 0 ; k < K ; k++) { if(!g->nextcol(g, &sm, 0, NA_ACTION_PROPORTIONAL)) return FAILURE; /* First compute the intercept. When all other variables * are zero, the intercept is just inv(mean(y)) for a suitable inv() * function depending on the loss */ s = 0; for(i = n1 ; i >= 0 ; --i) s += g->y[n * k + i]; beta_new = inv_func(s / n); updateloss(g, 0, beta_new, sm.x, 0, k, 0, 0, 0); if(g->verbose) printf("[k=%d] intercept: %.15f (%d samples)\n", k, beta_new, n); /* find smallest lambda1 that makes all coefficients zero, by * finding the largest z, but let the intercept affect lp * first because it's not penalised. */ for(j = 1 ; j < p1; j++) { jp1k = j + p1 * k; if(g->ignore[jp1k]) continue; if(!g->nextcol(g, &sm, j, NA_ACTION_PROPORTIONAL)) return FAILURE; step_func(&sm, g, k, &d1, &d2); s = fabs(d1 / d2); zmax = (zmax < s) ? s : zmax; g->grad_array[jp1k].value = s; } /* intercept */ g->beta[k * p1] = beta_new; } return zmax; }
/*! *@brief GarbageCollectionStart接口回调函数 *@author zhaohm3 *@param[in] jvmti_env *@retval *@note * *@since 2014-9-15 17:58 *@attention * */ GPrivate void JNICALL JVMGarbageCollectionStart(jvmtiEnv *jvmti_env) { GCMON_PRINT_FUNC(); if (NULL == gpPerfTree && gPerfMemory != NULL) { //! 通过gPerfMemory构建性能树 gcmon_init_perf_tree(); //! 性能计数器红黑树构建好了后,随机初始化性能采样项 GASSERT(gpPerfTree != NULL); sample_init(gpPerfTree); gpThread = os_thread_create((Int32_t (JNICALL *)(void *))sample_tdoit, (void *)"OSThread "); os_thread_start(gpThread); } }
void main() { sample S; int i = 0; sample_init(&S); while (i < 10) { sample_push(&S, i); i++; } for(i = 0; i < SAMPLE_SIZE; i++) { printf("%d\n",S.data[i]); } printf("Average is: %d\n", sample_average(&S)); }
/* Coordinate descent, multi-task * * This function will only work for quadratic functions like linear loss and * squared hinge loss, since it assumes that the Newton step for each variable * is exact. It's easy to modify it to loop over the Newton steps until some * convergence is achieved, e.g., for logistic loss. * */ int cd_gmatrix(gmatrix *g, step step_func, const int maxepochs, const int maxiters, const double lambda1, const double lambda2, const double gamma, const double trunc, int *numactiveK) { const int p1 = g->p + 1, K = g->K; int j, k, allconverged = 0, numactive = 0, epoch = 1, good = FALSE; double beta_new, delta, s; const double l2recip = 1 / (1 + lambda2); sample sm; double *beta_old = NULL; int *active_old = NULL; long pkj, p1K1 = p1 * K - 1, kK1; int v1, v2, e, l; int nE = K * (K - 1) / 2; double d1, d2, df1, df2, sv; double beta_pkj; double lossold = g->loss; int mult = 2, idx; int mx, i, m; double tmp; if(!sample_init(&sm)) return FAILURE; CALLOCTEST(beta_old, (long)p1 * K, sizeof(double)); CALLOCTEST(active_old, (long)p1 * K, sizeof(int)); //for(j = p1K1 ; j >= 0 ; --j) // active_old[j] = g->active[j] = !g->ignore[j]; while(epoch <= maxepochs) { numactive = 0; for(k = 0 ; k < K ; k++) { numactiveK[k] = 0; kK1 = k * (K - 1); for(j = 0 ; j < p1; j++) { pkj = (long)p1 * k + j; beta_pkj = g->beta[pkj]; beta_new = beta_old[pkj] = beta_pkj; if(g->active[pkj]) { if(!g->nextcol(g, &sm, j, NA_ACTION_PROPORTIONAL)) return FAILURE; step_func(&sm, g, k, &d1, &d2); /* don't penalise intercept */ if(j > 0) { /* fusion penalty */ df1 = 0; df2 = 0; if(g->dofusion) { /* for each task k, compute derivative over * the K-1 other tasks, as each task has K-1 pairs */ for(l = K - 2 ; l >= 0 ; --l) { e = g->edges[l + kK1]; v1 = g->pairs[e]; v2 = g->pairs[e + nE]; sv = g->beta[j + v1 * p1] * g->C[e + v1 * nE] + g->beta[j + v2 * p1] * g->C[e + v2 * nE]; df1 += sv * g->C[e + k * nE]; } /* derivatives of fusion loss */ df1 *= gamma; df2 = gamma * g->diagCC[k]; } s = beta_pkj - (d1 + df1) / (d2 + df2); beta_new = soft_threshold(s, lambda1) * l2recip; } else s = beta_new = beta_pkj - d1 / d2; /* numerically close enough to zero */ if(fabs(beta_new) < ZERO_THRESH) beta_new = 0; delta = beta_new - beta_pkj; if(delta != 0) updateloss(g, beta_pkj, delta, sm.x, j, k, lambda1, gamma, nE); /* intercept always deemed active */ g->active[pkj] = (j == 0) || (g->beta[pkj] != 0); } numactiveK[k] += g->active[pkj]; numactive += g->active[pkj]; } } g->loss = 0; for(i = 0 ; i < g->ncurr ; i++) { for(k = 0 ; k < g->K ; k++) { m = i + k * g->ncurr; tmp = (g->lp[m] - g->y[m]) * (g->lp[m] - g->y[m]); g->loss += tmp; } } g->loss /= (2.0 * g->ncurr); g->l1loss = 0; for(j = 1 ; j < p1 ; j++) for(k = 0 ; k < K ; k++) g->l1loss += fabs(g->beta[j + k * p1]); g->loss += lambda1 * g->l1loss; if(g->dofusion) { g->floss = 0; for(j = p1 - 1 ; j >= 1 ; --j) { for(e = nE - 1 ; e >= 0 ; --e) { v1 = g->pairs[e]; v2 = g->pairs[e + nE]; sv = g->beta[j + v1 * p1] * g->C[e + v1 * nE] + g->beta[j + v2 * p1] * g->C[e + v2 * nE]; g->floss += sv * sv; } } g->loss += gamma * 0.5 * g->floss; } if(fabs(g->loss - lossold) / fabs(lossold) < g->tol) allconverged++; else { allconverged = 0; //printf("%d loss: %.6f lossold: %.6f\n", epoch, g->loss, lossold); } if(allconverged == 1) { /* reset active-set to contain all (non monomorphic) coordinates, in * order to check whether non-active coordinates become active again * or vice-versa */ for(j = p1K1 ; j >= 0 ; --j) { active_old[j] = g->active[j]; g->active[j] = !g->ignore[j]; } if(g->verbose) { timestamp(); printf(" resetting activeset at epoch %d, loss: %.6f floss: %.6f\n", epoch, g->loss, g->floss); } mult = 2; } else if(allconverged == 2) { for(j = p1K1 ; j >= 0 ; --j) if(g->active[j] != active_old[j]) break; if(j < 0) { if(g->verbose) { timestamp(); printf(" terminating at epoch %d with %d active vars\n", epoch, numactive); } good = TRUE; break; } if(g->verbose) { timestamp(); printf(" active set changed, %d active vars, mx:", numactive); } /* keep iterating over existing active set, keep track * of the current active set */ for(j = p1K1 ; j >= 0 ; --j) active_old[j] = g->active[j]; /* double the size of the active set */ for(k = 0 ; k < K ; k++) { mx = fminl(mult * numactiveK[k], p1); printf("%d ", mx); for(j = mx - 1 ; j >= 0 ; --j) { idx = g->grad_array[j + k * p1].index + k * p1; g->active[idx] = !g->ignore[idx]; } } printf("\n"); allconverged = 0; mult *= 2; } epoch++; lossold = g->loss; } if(g->verbose) printf("\n"); FREENULL(beta_old); FREENULL(active_old); return good ? numactive : CDFAILURE; }
int points_init(void) { int i; // iterators for(i = 0; i < npoints; i++) { // initialize velocity and acceleration to zero points[i].u = 0.; points[i].v = 0.; points[i].w = 0.; points[i].u0 = 0.; points[i].v0 = 0.; points[i].w0 = 0.; points[i].udot = 0.; points[i].vdot = 0.; points[i].wdot = 0.; /* set initial position of point_point_particle reference basis to match the global * domain basis */ // initialize the hydrodynamic forces and moments to zero points[i].Fx = 0.; points[i].Fy = 0.; points[i].Fz = 0.; // initialize the point_point_particle interaction force to zero points[i].iFx = 0.; points[i].iFy = 0.; points[i].iFz = 0.; //TODO change ms initialization in the future // points[i].ms = 4*PI/3.0f *points[i].r*points[i].r*points[i].r*points[i].rho; real m=4*PI/3.0f *points[i].r*points[i].r*points[i].r*points[i].rho; points[i].ms = m; //printf("\npoint %d %f %f %f %f\n",i,sc_init_percent,m,points[i].r,points[i].rho); // points[i].x=(float)rand()/(float)(RAND_MAX/2)-1; // points[i].y=(float)rand()/(float)(RAND_MAX/2)-1; // points[i].z=0; points[i].x0 =points[i].x; points[i].y0 =points[i].y; points[i].z0 =points[i].z; points[i].ms0 =points[i].ms ; points[i].msdot =0; points[i].hp =0; points[i].xi=points[i].x; points[i].yi=points[i].y; points[i].zi=points[i].z; // printf("x y z r rho %f %f %f %f %f\n",points[i].xi,points[i].yi,points[i].zi,points[i].r,points[i].rho); //point id points[i].id = i+1; //index of grid that the particle reside in points[i].i = 0; points[i].j = 0; points[i].k = 0; //point iteration sub-timestep points[i].dt = points[i].rho *2.f*points[i].r*points[i].r/(9.0f*mu); } sample_init(); return EXIT_SUCCESS; }