int main() { std::cout << std::endl; std::cout << "***********************************************************" << std::endl; std::cout << " Mutex" << std::endl; std::cout << "***********************************************************" << std::endl; std::cout << std::endl; std::cout << "Lock" << std::endl; mutex.lock(); std::cout << "Unlock" << std::endl; mutex.unlock(); std::cout << "Trylock" << std::endl; mutex.trylock(); std::cout << "Try to lock block already locked" << std::endl; if (mutex.trylock()) { std::cout << "Abnormal" << std::endl; } else { std::cout << "Failed(Normal)" << std::endl; } std::cout << "Unlock" << std::endl; mutex.unlock(); std::cout << std::endl; std::cout << "***********************************************************" << std::endl; std::cout << " Synchronization" << std::endl; std::cout << "***********************************************************" << std::endl; std::cout << std::endl; ecl::Thread thread_f(f); ecl::Sleep sleep; std::cout << "Main: enter blocking" << std::endl; mutex.lock(); sleep(3); if (shared_value < 5) { std::cout << "shared_value(" << shared_value << ") is smaller than 5" << std::endl; } else { std::cout << "shared_value(" << shared_value << ") is larger than 5" << std::endl; } mutex.unlock(); std::cout << "Main: leave blocking" << std::endl; thread_f.join(); std::cout << std::endl; std::cout << "***********************************************************" << std::endl; std::cout << " Passed" << std::endl; std::cout << "***********************************************************" << std::endl; std::cout << std::endl; return 0; }
void prime_pthread() { int res; size_t mem = sizeof(int)*(n + 1); int *has = malloc(mem); memset(has, MAY_BE_PRIME, mem); int num_cpus = sysconf(_SC_NPROCESSORS_ONLN); int i; pthread_t a_thread[num_cpus]; arg_type *arg = calloc(num_cpus, sizeof(arg_type)); Thread_Funcition thread_f = find_prime2; for (i = 1; i < num_cpus; i++) { arg[i].has = has; arg[i].num_cpus = num_cpus; arg[i].start = i + START; // argument will be sent to thread directly, // so use malloc or static res = pthread_create(&a_thread[i], NULL, thread_f, arg+i); if (res != 0) { perror(""); exit(EXIT_FAILURE); } } i = 0; arg[i].has = has; arg[i].num_cpus = num_cpus; arg[i].start = i + START; thread_f(arg+i); for (i = 1; i < num_cpus; i++) { pthread_join(a_thread[i], NULL); } //output(has, n); free(has); }
void CART::fit(Problem const &prob, std::vector<float> const &R, std::vector<float> &F1) { uint32_t const nr_field = prob.nr_field; uint32_t const nr_sparse_field = prob.nr_sparse_field; uint32_t const nr_instance = prob.nr_instance; std::vector<Location> locations(nr_instance); #pragma omp parallel for schedule(static) for(uint32_t i = 0; i < nr_instance; ++i) locations[i].r = R[i]; for(uint32_t d = 0, offset = 1; d < max_depth; ++d, offset *= 2) { uint32_t const nr_leaf = static_cast<uint32_t>(pow(2, d)); std::vector<Meta> metas0(nr_leaf); for(uint32_t i = 0; i < nr_instance; ++i) { Location &location = locations[i]; if(location.shrinked) continue; Meta &meta = metas0[location.tnode_idx-offset]; meta.s += location.r; ++meta.n; } std::vector<Defender> defenders(nr_leaf*nr_field); std::vector<Defender> defenders_sparse(nr_leaf*nr_sparse_field); for(uint32_t f = 0; f < nr_leaf; ++f) { Meta const &meta = metas0[f]; double const ese = meta.s*meta.s/static_cast<double>(meta.n); for(uint32_t j = 0; j < nr_field; ++j) defenders[f*nr_field+j].ese = ese; for(uint32_t j = 0; j < nr_sparse_field; ++j) defenders_sparse[f*nr_sparse_field+j].ese = ese; } std::vector<Defender> defenders_inv = defenders; std::thread thread_f(scan, std::ref(prob), std::ref(locations), std::ref(metas0), std::ref(defenders), offset, true); std::thread thread_b(scan, std::ref(prob), std::ref(locations), std::ref(metas0), std::ref(defenders_inv), offset, false); scan_sparse(prob, locations, metas0, defenders_sparse, offset, true); thread_f.join(); thread_b.join(); for(uint32_t f = 0; f < nr_leaf; ++f) { Meta const &meta = metas0[f]; double best_ese = meta.s*meta.s/static_cast<double>(meta.n); TreeNode &tnode = tnodes[f+offset]; for(uint32_t j = 0; j < nr_field; ++j) { Defender defender = defenders[f*nr_field+j]; if(defender.ese > best_ese) { best_ese = defender.ese; tnode.feature = j; tnode.threshold = defender.threshold; } defender = defenders_inv[f*nr_field+j]; if(defender.ese > best_ese) { best_ese = defender.ese; tnode.feature = j; tnode.threshold = defender.threshold; } } for(uint32_t j = 0; j < nr_sparse_field; ++j) { Defender defender = defenders_sparse[f*nr_sparse_field+j]; if(defender.ese > best_ese) { best_ese = defender.ese; tnode.feature = nr_field + j; tnode.threshold = defender.threshold; } } } #pragma omp parallel for schedule(static) for(uint32_t i = 0; i < nr_instance; ++i) { Location &location = locations[i]; if(location.shrinked) continue; uint32_t &tnode_idx = location.tnode_idx; TreeNode &tnode = tnodes[tnode_idx]; if(tnode.feature == -1) { location.shrinked = true; } else if(static_cast<uint32_t>(tnode.feature) < nr_field) { if(prob.Z[tnode.feature][i].v < tnode.threshold) tnode_idx = 2*tnode_idx; else tnode_idx = 2*tnode_idx+1; } else { uint32_t const target_feature = static_cast<uint32_t>(tnode.feature-nr_field); bool is_one = false; for(uint64_t p = prob.SJP[i]; p < prob.SJP[i+1]; ++p) { if(prob.SJ[p] == target_feature) { is_one = true; break; } } if(!is_one) tnode_idx = 2*tnode_idx; else tnode_idx = 2*tnode_idx+1; } } } std::vector<std::pair<double, double>> tmp(max_tnodes, std::make_pair(0, 0)); for(uint32_t i = 0; i < nr_instance; ++i) { float const r = locations[i].r; uint32_t const tnode_idx = locations[i].tnode_idx; tmp[tnode_idx].first += r; tmp[tnode_idx].second += fabs(r)*(1-fabs(r)); } for(uint32_t tnode_idx = 1; tnode_idx <= max_tnodes; ++tnode_idx) { double a, b; std::tie(a, b) = tmp[tnode_idx]; tnodes[tnode_idx].gamma = (b <= 1e-12)? 0 : static_cast<float>(a/b); } #pragma omp parallel for schedule(static) for(uint32_t i = 0; i < nr_instance; ++i) F1[i] = tnodes[locations[i].tnode_idx].gamma; }