how can i optimize the multi-threading

Viewed 30

I currently am trying to create a vector and fill it with random values( for this case the random values will be 1) once the vector is created i want to calculate the total sum which should be 100000000, I am using ones to test that the addition was done correctly.

My goal is to make this code more efficient and run faster, i would appreciate any suggestions

#include <chrono>
#include <iostream>
#include <mutex>
#include <random>
#include <thread>
#include <vector>
#include <atomic>

// Constant vector size; 100,000,000
constexpr long long SIZE = 100000000;

// Dividing the vector into 4 equal parts
// first quarter: 0 --> size/4
constexpr long long FIRST_PART = (SIZE /4);
// second quarter: size/4 --> size/2
constexpr long long SECOND_PART = 2*(SIZE / 4);
// third quarter: size/2 --> 3*(size/4)
constexpr long long THIRD_PART = 3*(SIZE / 4);
// fourth quarter: 3*(size/4) --> end (size)
constexpr long long FOURTH_PART = SIZE;

// Global mutex to guard the shared variable
std::mutex myMutex;

Here is my function to total the vector

void findSum(unsigned long long& sum,
                const std::vector<int>& myVector,
                    unsigned long long begin,
                        unsigned long long end) {
    for (auto it = begin; it < end; ++it) {
        // create a lock to guard the shared variable
        // no need to unlock the lock "myLock" when using luck_guard
        // it is unlocked implicitly after executing the statement.

        std::lock_guard<std::mutex> myLock(myMutex);
        //std::atomic<std::atomic<int64_t>> myattempt();
        // what is a mutex
        //mutex xmutex;
        sum += myVector[it];
    }
}

Below is my main function where i try to use multi-threading

int main() {

    // start timers
    auto startTime = std::chrono::system_clock::now();
    auto startTotalTime = std::chrono::system_clock::now();

    //std::cout << "Start variable data type is: " << typeid(start).name() << std::endl;

    std::cout << "Creating the Vector" << std::endl;

    // create a vector of random variables
    std::vector<int> randValues;
    randValues.reserve(SIZE);
    
    
    std::cout << "Filling the Vector" << std::endl;

    // use the Mersenne Twister algorithm to generate random numbers
    std::mt19937 rndEngine;
    std::uniform_int_distribution<> uniformDist(1, 1);
    for (long long i = 0; i < SIZE; i++)
        randValues.push_back(uniformDist(rndEngine));
    


    // stop the timer and calculate the elapsed time
    std::chrono::duration<double> elapsedTime = std::chrono::system_clock::now() - startTime;
    std::cout << "Time for creating and filling the vector: " << elapsedTime.count() << " seconds" << std::endl;

    std::cout << "Starting the work ... Calculating the sum ..." << std::endl;

    unsigned long long totalSum = 0;

    // reset the timer
    startTime = std::chrono::system_clock::now();

    // finding the sum without using threads
    //findSum(std::ref(totalSum), std::ref(randValues), 0, SIZE);
    
    std::thread t1(findSum, std::ref(totalSum), std::ref(randValues), 0, FIRST_PART);
    std::thread t2(findSum, std::ref(totalSum), std::ref(randValues), FIRST_PART, SECOND_PART);
    std::thread t3(findSum, std::ref(totalSum), std::ref(randValues), SECOND_PART, THIRD_PART);
    std::thread t4(findSum, std::ref(totalSum), std::ref(randValues), THIRD_PART, FOURTH_PART);

    t1.join();
    t2.join();
    t3.join();
    t4.join();
    
    // stop the timer and get the elapsed time
    elapsedTime = std::chrono::system_clock::now() - startTime;
    std::cout << "Time for addition " << elapsedTime.count() << " seconds" << std::endl;
    std::cout << "Total Sum Result: " << totalSum << std::endl;

    // stop the total timer and calculate the elapsed time for the entire program
    std::chrono::duration<double> elapsedTotalTime = std::chrono::system_clock::now() - startTotalTime;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "The time for completing the entire task is: " << elapsedTotalTime.count() << " seconds" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;
    std::cout << "--------------------------------------------------------------" << std::endl;

    
    return 0;
}
0 Answers
Related