BASKER_INLINE int BaskerMatrix<Int,Entry,Exe_Space>::copy_values(Int _m, Int _n, Int _nnz, Int *_col_ptr, Int *_row_idx, Entry *_val) { if(nrow!=_m) { std::cout << "Error: Changed m size" << std::endl; std::cout << "old m: " << nrow << " new m: " << _m << std::endl; return BASKER_ERROR; } if(ncol==_n) { copy_vec(_col_ptr, _n+1, col_ptr); } else { std::cout << "Error: Changed n size" << std::endl; std::cout << "old n: " << ncol << " new n: " << _n << std::endl; return BASKER_ERROR; } if(nnz == _nnz) { copy_vec(_row_idx, _nnz, row_idx); copy_vec(_val,_nnz, val); } else { std::cout << "Error: Changed number of entries " << std::endl; std::cout << "old nnz: " << nnz << " new nnz: " << _nnz << std::endl; return BASKER_ERROR; } return 0; }//end copy_values()
BASKER_INLINE int BaskerMatrix<Int,Entry,Exe_Space>::copy_values(Int _m, Int _n, Int _nnz, Int *_col_ptr, Int *_row_idx, Entry *_val) { if(nrow!=_m) { printf("Error: Changed m size\n"); return BASKER_ERROR; } if(ncol==_n) { copy_vec(_col_ptr, _n+1, col_ptr); } else { printf("ERROR: Changed size \n"); return BASKER_ERROR; } if(nnz == _nnz) { copy_vec(_row_idx, _nnz, row_idx); copy_vec(_val,_nnz, val); } else { printf("ERROR: Changed number of entries \n"); return BASKER_ERROR; } return 0; }//end copy_values()
BASKER_INLINE void BaskerMatrix<Int,Entry,Exe_Space>::init_matrix ( string _label, Int _m, Int _n, Int _nnz, Int *_col_ptr, Int *_row_idx, Entry *_val ) { label = _label; init_vectors(_m, _n, _nnz); copy_vec(_col_ptr, _n+1, col_ptr); copy_vec(_row_idx, _nnz, row_idx); copy_vec(_val,_nnz, val); }//end init_matrix(string with ptrs)
float dnn::compute_loss(float *** weights, float ** biases) { float ** z=new float*[num_layers]; for(int i=0;i<num_layers;i++) z[i]=new float[num_units_ineach_layer[i]]; double loss=0; int cnt=0; for(int smp=0;smp<num_train_data;smp++) { if(((rand()%100000)/100000.0)>(num_smps_evaluate*1.0/num_train_data)) continue; //copy the first layer copy_vec(z[0], input_features[smp], num_units_ineach_layer[0]); //forward propagation for(int i=1;i<num_layers;i++) forward_activation(i-1, weights[i-1], biases[i-1], z[i-1], z[i]); //compute cross entropy loss loss+=compute_cross_entropy_loss(z[num_layers-1], smp); cnt++; } loss/=cnt; for(int i=0;i<num_layers;i++) delete[]z[i]; delete[]z; return loss; }
int main() { unsigned int adc; float vol; int n; copy_vec(); irq_init(); button_init(); uart0_init(); timer_init(); // timer4_init(); lcd_init(); lcd_clean(0xffff); adc_ts_init(); ts_init(); while(1); while(1) { adc = read_adc(0); vol = adc * 3.3 / 0x3ff; n = (vol - (int)vol) * 1000; printf("AIN0: adc = %u voltage:%d.%03d\r\n", adc, (int)vol, n); delayms(1000); } return 0; }
/* void update_paths(void) * Used after completing calls to vit_dec_ACS * -hardcoded with unrolled loop for 4 state encoder * -only part that needs to be manually adjusted for other encoders */ void update_paths(/*short i*/) { //short i; /* for(i = 0; i < NUM_STATES; i++) { pm[i] = pm_next[i]; paths[i] = paths_next[i]; }*/ pm[0] = pm_next[0]; pm[1] = pm_next[1]; pm[2] = pm_next[2]; pm[3] = pm_next[3]; copy_vec(paths[0], paths_next[0]); copy_vec(paths[1], paths_next[1]); copy_vec(paths[2], paths_next[2]); copy_vec(paths[3], paths_next[3]); }
void dnn::compute_gradient_single_data(int idx_data,float *** local_weights, float ** local_biases, float *** delta_weights, float ** delta_biases, float ** z, float ** delta) { copy_vec(z[0], input_features[idx_data], num_units_ineach_layer[0]); //forward propagation for(int i=1;i<num_layers;i++) forward_activation(i-1, local_weights[i-1], local_biases[i-1], z[i-1], z[i]); //backward propagation compute_error_output_layer(delta[num_layers-2], z[num_layers-1],idx_data); for(int l=num_layers-3;l>=0;l--) backward_error_computation(l, local_weights[l+1], z[l+1], delta[l], delta[l+1]); //accumulate gradient of weights matrices and bias vectors for(int l=0;l<num_layers-1;l++){ int dim1=num_units_ineach_layer[l+1], dim2=num_units_ineach_layer[l]; for(int j=0;j<dim1;j++){ for(int i=0;i<dim2;i++) delta_weights[l][j][i]+=delta[l][j]*z[l][i]; } } for(int l=1;l<num_layers;l++){ int dim=num_units_ineach_layer[l]; for(int j=0;j<dim;j++) delta_biases[l-1][j]+=delta[l-1][j]; } }
BASKER_INLINE int BaskerMatrix<Int,Entry,Exe_Space>::copy_values ( Int _sr, Int _m, Int _sc, Int _n, Int _nnz, Int *_col_ptr, Int *_row_idx, Entry *_val) { if(nrow!=_m) { printf("Error: Changed m size\n"); printf("old m: %d new m: %d \n", nrow , _m); return BASKER_ERROR; } if(ncol==_n) { copy_vec(_col_ptr, _n+1, col_ptr); } else { printf("ERROR: Changed n size \n"); printf("old n: %d new n: %d \n", ncol, _n); return BASKER_ERROR; } if(nnz == _nnz) { copy_vec(_row_idx, _nnz, row_idx); copy_vec(_val,_nnz, val); } else { printf("ERROR: Changed number of entries \n"); printf("old nnz: %d new nnz: %d \n", nnz, _nnz); return BASKER_ERROR; } srow = _sr; erow = srow + _m; scol = _sc; ecol = scol + _n; return 0; }//end copy_values()
void rbm::cd_single_data(float * features_idx, float ** weights, float * hidden_bias, float * visible_bias, float ** delta_weights, float * delta_hidden_bias, float * delta_visible_bias, int K){ float * v = new float[num_visible_units]; float * h = new float[num_hidden_units]; float * p_h = new float[num_hidden_units]; float * p_v = new float[num_visible_units]; float * p_0 = new float[num_hidden_units]; float * wvc = new float[num_hidden_units]; float * whb = new float[num_visible_units]; copy_vec(v, features_idx, num_visible_units); // compute wv0 multiply_matrix_vector(weights, v, wvc, num_hidden_units, num_visible_units); // compute wvc0 add_vector(wvc, hidden_bias, num_hidden_units); // compute p_0 compute_probability_hidden_given_visible(wvc, p_0, num_hidden_units); // do K-step gibbs sampling for (int i=0; i<K; i++){ // compute wv multiply_matrix_vector(weights, v, wvc, num_hidden_units, num_visible_units); // compute wvc add_vector(wvc, hidden_bias, num_hidden_units); compute_probability_hidden_given_visible(wvc, p_h, num_hidden_units); sample_vector(h, p_h, num_hidden_units); // compute wh multiply_matrix_vector_column_wise(weights, h, whb, num_hidden_units, num_visible_units); // compute whb add_vector(whb, visible_bias, num_visible_units); compute_probability_visible_given_hidden(whb, p_v, num_visible_units); sample_vector(v, p_v, num_visible_units); } update_weights_gradient(delta_weights, features_idx, v, p_0, p_h, num_hidden_units, num_visible_units); update_visible_bias_gradient(delta_visible_bias, features_idx, v, num_visible_units); update_hidden_bias_gradient(delta_hidden_bias, p_0, p_h, num_hidden_units); delete[] v; delete[] h; delete[] p_h; delete[] p_v; delete[] p_0; delete[] wvc; delete[] whb; }
void rk2(model_t model, double *x0, double params[], FILE *fp, double *_t){ int i, n = model.n_state; double x_next[MAX_STATE], x[MAX_STATE]; copy_vec(&x[0], x0, n); double h = 1e-3; double t = 0.0; fprint_vec(fp, t, x, n); for (i=0; !model.stop(t, x, params); i++){ rk2_step(model.f, &x_next[0], &x[0], n, h, params); copy_vec(&x[0], &x_next[0], n); t = (i+1)*h; fprint_vec(fp, t, x, n); } if (_t) *_t = t; }
double quantile (const VEC v, const double q){ assert(NULL!=v); assert(q>=0. && q<=1.); VEC vcopy = copy_vec(v); qsort (vcopy->x,vlen(vcopy),sizeof(double),CmpDouble); const double med = quantile_fromsorted(vcopy,q); free_vec(vcopy); return med; }
/* Some unnecessary vector copies when this function is combined with median */ double mad ( const VEC v){ assert(NULL!=v); const double vmedian = median(v); VEC vcopy = copy_vec (v); const unsigned int len = vlen(vcopy); for ( unsigned int i=0 ; i<len ; i++){ vset(vcopy,i,fabs(vget(vcopy,i)-vmedian)); } const double devmedian = median(vcopy); free_vec(vcopy); return devmedian * MADSCALE; }
/* void vit_dec_get(bv_t) * Returns the minimum weight path so far * -Note that an O(n) search is here where an O(log(n)) would be better * -This function should be updated for better efficiency with more states */ void vit_dec_get(bv_t dest) { int min_index = 0; int min_value = pm[0]; int i; for(i = 1; i < NUM_STATES; i++) { if(pm[i] < min_value) { min_value = pm[i]; min_index = i; } } copy_vec(dest, paths[min_index]); }
/* vit_dec_ACS(short) * Add Compare Select Step * - Given branch metrics (based on hard or soft decoding) * - Find path metrics and survivor paths * - Must be called for each state but split up for interrupt compatibility */ void vit_dec_ACS(short i) { //short i; //generate path metrics using ACS //for(i = 0; i < NUM_STATES; i++) {h) //get candidates short index1 = InverseTransitions[i][0]; int path1 = pm[index1] + bm[Trellis[index1][i]]; short index2 = InverseTransitions[i][1]; int path2 = pm[index2] + bm[Trellis[index2][i]]; //printf("i=%d with index1: %d, Decode: %d, pm: %d, bm: %d, path1: %d \n", i, // index1, Decode[index1][i], pm[index1], bm[Trellis[index1][i]], path1); // printf("i=%d with index2: %d, Decode: %d, pm: %d, bm: %d, path2: %d \n", i, // index2, Decode[index2][i], pm[index2], bm[Trellis[index2][i]], path2); short decoded_bit; //compare and select, update survivor paths if(path1 <= path2) { decoded_bit = Decode[index1][i]; pm_next[i] = path1; copy_vec(paths_next[i], paths[index1]); } else { decoded_bit = Decode[index2][i]; pm_next[i] = path2; copy_vec(paths_next[i], paths[index2]); } bit_append(paths_next[i], decoded_bit); //print_vec(paths_next[i]); //} //update_paths(); }
VEC quantiles ( const VEC v, const VEC q){ assert(NULL!=q); assert(NULL!=v); VEC quant = create_vec(vlen(q)); assert(NULL!=quant); VEC vcopy = copy_vec(v); qsort (vcopy->x,vlen(vcopy),sizeof(double),CmpDouble); for ( unsigned int i=0; i<vlen(q) ; i++){ assert(vget(q,i)>=0. && vget(q,i)<=1.); vset(quant,i,quantile_fromsorted(vcopy,vget(q,i))); } free_vec(vcopy); return quant; }
int main() { copy_vec(); irq_init(); button_init(); uart0_init(); timer_init(); // timer4_init(); lcd_init(); lcd_clean(0xffff); adc_ts_init(); ts_init(); // tslib_calibrate(); // lcd_clean(0xffff); nand_init(); #if 1 nand_read_id(); while(1); #endif printf("\r\n\n"); printf("===============================================\r\n"); printf(" NAND FLASH PROGRAMMING \r\n"); unsigned int size = 512 * 1024; // 512KB nand_read_id(); printf("erase entire flash, waiting ... \r\n"); int i; for(i = 0; i < 2048; i++) nand_erase_block(i); printf("start program ... \r\n"); nand_write_bytes(0x0, (unsigned char *)0x31000000, size); printf("program end ... \r\n"); printf("===============================================\r\n"); while(1); return 0; }
int dnn::predict_single_data(int idx_data,float *** local_weights, float ** local_biases, float ** z) { copy_vec(z[0], input_features[idx_data], num_units_ineach_layer[0]); //forward propagation for(int i=1;i<num_layers;i++) forward_activation(i-1, local_weights[i-1], local_biases[i-1], z[i-1], z[i]); int maxid=-1; float maxv=-1; for(int i=0;i<num_units_ineach_layer[num_layers-1];i++){ //std::cout<<z[num_layers-1][i]<<" "; if(z[num_layers-1][i]>maxv){ maxv=z[num_layers-1][i]; maxid=i; } } //std::cout<<std::endl; //std::cout<<maxid<<" "<<maxv<<std::endl; return maxid; }
void prepare_householder_vector( viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT>& A, viennacl::vector<SCALARTYPE, ALIGNMENT>& D, vcl_size_t size, vcl_size_t row_start, vcl_size_t col_start, vcl_size_t start, bool is_column ) { boost::numeric::ublas::vector<SCALARTYPE> tmp = boost::numeric::ublas::scalar_vector<SCALARTYPE>(size, 0); copy_vec(A, D, row_start, col_start, is_column); fast_copy(D.begin(), D.begin() + vcl_ptrdiff_t(size - start), tmp.begin() + start); //std::cout << "1: " << tmp << "\n"; detail::householder_vector(tmp, start); fast_copy(tmp, D); //std::cout << "2: " << D << "\n"; }
static int check_long (model_t model, int g, int *src, TransitionCB cb, void *context) { check_ctx_t *ctx = GBgetContext (model); int count; int found; // collect ctx->src = src; ctx->group = g; ctx->user_cb = cb; ctx->user_ctx = context; ci_clear (ctx->check_must); isba_discard_int (ctx->stack, isba_size_int(ctx->stack)); HREassert (!ctx->reentrent, "INTERFACE ERROR: GBgetTransitions* is not re-entrant"); ctx->reentrent = 1; count = GBgetTransitionsLong (ctx->parent, g, src, collect, ctx); ctx->reentrent = 0; found = isba_size_int(ctx->stack); abort_if (count != found, "Wrong count returned by GBnextLong(collect): %d (Found: %d).", count, found); // compare ctx->src2 = copy_vec (ctx, g, src); ctx->comparison_failed = false; ctx->call_idx = 0; count = GBgetTransitionsLong (ctx->parent, g, ctx->src2, compare, ctx); abort_if (count != ctx->call_idx , "Wrong count returned by GBnextLong(compare): %d (Found: %d).", count, ctx->call_idx ); if (ctx->call_idx != found || ctx->comparison_failed) { find_culprit_slot (ctx, g); } return count; }
void copy_vec4(vec4 a, vec4 b) { copy_vec(a,b,4); }
void copy_vec3(vec3 a, vec3 b) { copy_vec(a,b,3); }
void copy_mat(float ** a, float ** b, int dim1, int dim2) { for(int i=0;i<dim1;i++) copy_vec(a[i], b[i], dim2); }