Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Mihai Anton on 08/10/2019.
- //
- #include <thread>
- #include <iostream>
- #include "numberSum.h"
- using namespace std;
- void printVec(vector<int> v){
- for(int i:v){
- cout << i << " ";
- }
- cout << "\n";
- }
- void numberSumSequential(vector<int>* a, vector<int>* b,vector<int>* result){
- int carry = 0, sum = 0;
- for(int i=0;i<(*a).size();i++){
- sum = (*a)[i] + (*b)[i] + carry;
- (*result)[i] = sum%10;
- carry = sum/10;
- }
- if(carry){
- (*result).push_back(1);
- }
- }
- void threadWork(vector<int>* a, vector<int>* b, vector<int>* result, int start,int stop,bool* extra){
- bool done = false;
- while(start < stop && !done){
- if(((*a)[start] + (*b)[start]) > 9){ //current position causes carry
- if(start+1<result->size()) {
- (*result)[start + 1] = ((*a)[start+1] + (*b)[start+1] + 1)%10;
- }
- else if(start+1==result->size()){
- *extra = true;
- return;
- }
- start += 1;
- while(((*a)[start] + (*b)[start]) == 9){ //while there are chained maybes after the carry
- if(start + 1 < result->size()){
- (*result)[start + 1] = ((*a)[start+1] + (*b)[start+1] + 1)%10;
- }
- else if(start + 1 == result->size()){
- *extra = true;
- return;
- }
- start += 1;
- }
- }
- else{ //maybe (not coming after carry) or never
- if(start+1<result->size() && (*result)[start + 1]==-1) {
- (*result)[start + 1] = ((*a)[start+1] + (*b)[start+1])%10;
- }
- start += 1;
- }
- }
- }
- void numberSumParallel(vector<int>* a, vector<int>* b,vector<int>* result,int threadCount,bool debug = false) {
- vector<thread> threads(threadCount);
- bool extra = false;
- int sharePerThread = int(result->size()/threadCount);
- int remain = result->size()%threadCount;
- int start = 0;
- int stop = 0;
- for(int i=0;i<threadCount;i++){
- stop = start + sharePerThread;
- if(remain > 0){
- stop ++;
- remain --;
- }
- threads[i] = thread(threadWork,a,b,result,start,stop,&extra);
- start = stop;
- }
- for(int i=0;i<threadCount;i++){
- threads[i].join();
- }
- (*result)[0] = ((*a)[0] + (*b)[0])%10;
- if(extra){
- (*result).push_back(1);
- }
- // //step1: mark Never(0), Carry(2) and Maybe(1)
- //
- // vector<thread> threads;
- // for(int i=0;i<threadCount;i++){
- // thread th(threadStep1,a,b,result,i,threadCount);
- // threads.push_back(move(th));
- // }
- //
- // for(int i=0;i<threadCount;i++){
- // threads[i].join();
- // }
- //
- // if(debug) {
- // cout << "s1 ";
- // printVec(*result);
- // cout << "\n";
- // }
- //
- //// ///step2:
- //// threads.clear();
- //// for(int i=0;i<threadCount;i++){
- //// thread th(threadStep2,result,aux,i+1,threadCount);
- //// threads.push_back(move(th));
- //// }
- ////
- //// for(int i=0;i<threadCount;i++){
- //// threads[i].join();
- //// }
- //// (*aux)[0] = (*result)[0];
- ////
- //// if(debug) {
- //// cout << "s2 ";
- //// printVec(*aux);
- //// cout << "\n";
- //// }
- // for(int i=0;i<result->size();i++){
- // (*aux)[i] = (*result)[i];
- // }
- //
- // ///step3: setting 1 for each carry
- // threads.clear();
- // for(int i=0;i<threadCount;i++){
- // thread th(threadStep3,aux,result,i,threadCount);
- // threads.push_back(move(th));
- // }
- //
- // for(int i=0;i<threadCount;i++){
- // threads[i].join();
- // }
- // (*result)[0] = 0;
- // if(debug) {
- // cout << "s3 ";
- // printVec(*result);
- // cout << "\n";
- // }
- //
- // bool extraCarry = false;
- // if((*aux)[(*aux).size()-1] == 2){
- // extraCarry = true;
- // }
- //
- // ///step4: sum a + b + result(that keeps the carry for each position)
- // threads.clear();
- // for(int i=0;i<threadCount;i++){
- // thread th(threadStep4,a,b,result,i,threadCount);
- // threads.push_back(move(th));
- // }
- //
- // for(int i=0;i<threadCount;i++){
- // threads[i].join();
- // }
- //
- // if(extraCarry){
- // (*result).push_back(1);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement