std::future<result_of_t<decay_t<F>(decay_t<A>)>> spawn_task(F &&f, A &&a) { using result_t = result_of_t<decay_t<F>(decay_t<A>)>; std::packaged_task< result_t(decay_t<A>)> task(std::forward<F>(f)); auto res = task.get_future(); std::thread t(std::move(task), std::forward<A>(a)); t.detach(); return res; }
double run(Fun fun, size_t count, Dump & dump) { using result_t = decltype(fun()); result_t result = result_t(); auto start = Clock::now(); for (size_t i = 0; i < count; ++i) { result += fun(); } auto end = Clock::now(); dump << result; std::chrono::duration<double> elapsed = end - start; return elapsed.count(); }
int doWork(MPI_Datatype* ResultMpiType,const int k){ int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); int MAX_RESULT_SIZE = k; char msg[MAX_MSG_SIZE]; float cmpData[SEARCH_VECTOR_SIZE]; double times[2]; MPI_Status status; Timing parserTime; Timing searchTime; Timing workWallTime; workWallTime.start(); std::chrono::duration<double> read_time_elapse; MPI_Recv(cmpData, SEARCH_VECTOR_SIZE, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status); if(status.MPI_TAG != SEARCH_VECTOR){ std::cout<< rank << " recieved terminate signal" << std::endl; return 0; } while (1) { // Receive a message from the master MPI_Recv(msg, /* message buffer */ MAX_MSG_SIZE, /* buffer size */ MPI_CHAR, /* data item is an integer */ 0, /* Receive from master */ MPI_ANY_TAG, MPI_COMM_WORLD, /* default communicator */ &status); // Check if we have been terminated by the master // exit from the worker loop if (status.MPI_TAG == TERMINATE) { std::cout<< rank << " recieved terminate signal" << std::endl; return 0; } std::shared_ptr<MapString_t> nameMap(new MapString_t); std::vector<result_t> results; std::shared_ptr<std::vector<float>> dataVector(new std::vector<float>); Parser p(nameMap,dataVector); parserTime.start(); if(!p.parse_file(msg,&read_time_elapse)){ std::cerr<<"could not parse file: "<<msg<<std::endl; return 0; } parserTime.end(); searchTime.start(); int lineLength = p.get_line_length(); int index = 0; for(auto& file : *nameMap){ results.push_back(result_t()); results.at(index).distance = findDist(lineLength, dataVector,file.second,cmpData); strcpy(results.at(index).fileName,file.first.c_str()); index++; } std::sort(results.begin(),results.end(),resultPairSort); results.resize(MAX_RESULT_SIZE); searchTime.end(); MPI_Send(results.data(), /* message buffer */ MAX_RESULT_SIZE, /* buffer size */ *ResultMpiType, /* data item is an integer */ 0, /* destination process rank, the master */ RESULTS, /* user chosen message tag */ MPI_COMM_WORLD); workWallTime.end(); //add the times to an array to be sent to the master times[0] = parserTime.get_elapse(); times[1] = searchTime.get_elapse(); times[2] = workWallTime.get_elapse(); //send out the timing results to be compiled MPI_Send(times, NUM_TIME_RESULTS, MPI_DOUBLE, 0, TIMING, MPI_COMM_WORLD); } return 0; }