multithreading - C++ trying to improve performance of pthread program -
i need improving speed of multithread program in c++ using pthreads.
std::vector<double> solve_progon(std::vector<std::vector<double> > a, std::vector <double> b) { // solving } std::vector<double> solve(std::vector<double> left, std::vector<double> mid, std::vector<double> right, int j) { //solving } void * calc(void *thread) { long t = (long) thread; int start_index = t * (x_size / threads); int end_index = (t != threads - 1)?(t + 1) * (x_size / threads) - 1: x_size - 1; std::vector<std::vector<double> > local, next; std::vector<double> zeros; (int = 0; < y_size; i++) { zeros.push_back(0); } double cur_time = 0; while (cur_time < t) { (int = start_index; <= end_index; ++) { next.push_back(solve(phi[i - 1], phi[i], phi[i + 1], - start_index)); } cur_time += dt; pthread_barrier_wait(&bar); (int = start_index; <=end_index; i++) { phi[i] = next[i - start_index]; } next.clear(); pthread_barrier_wait(&syn); } pthread_exit(null); } int main(int argc, char **argv) { //some init pthread_barrier_init(&bar, null, threads); pthread_barrier_init(&syn, null, threads); pthread_t *threads = new pthread_t[threads]; unsigned long long start = clock_time(); (long = 0; < threads; i++) { if (pthread_create(&threads[i], null, calc, (void *)i) != 0) { std::cout << "can't create thread " << << std::endl; } } (int = 0; < threads; i++) { pthread_join(threads[i], null); } std::cout << "it takes " << (double)(clock_time() - start) / 1e9 << std::endl; return 0; }
full version @ https://github.com/minaevmike/fedlab_pthread/blob/master/main.cpp f.e. if have 4 thread calculation time 118.288 sec
. if 1 101.993
. how can improve speed. thank you.
Comments
Post a Comment