Untitled

 avatar
unknown
plain_text
12 days ago
8.2 kB
3
Indexable
#include <iostream>
#include <unistd.h>

int main() {
    // Get the number of available CPU cores
    int numCores = sysconf(_SC_NPROCESSORS_ONLN);

    // Display the result
    std::cout << "Total number of CPU cores: " << numCores << std::endl;

    return 0;
}
Code 2
#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <sched.h>

using namespace std;

constexpr int ARRAY_SIZE = 100000; // Size of the arrays

// Function to perform addition operation
void additionOperation(const vector<int>& array) {
    int core = sched_getcpu();
    cout << "Addition operation is running on core: " << core << endl;

    // Perform addition
    long long sum = accumulate(array.begin(), array.end(), 0LL); // Use long long for sum
    cout << "Sum of the array elements: " << sum << endl;
}

// Function to perform multiplication operation
void multiplicationOperation(const vector<int>& array) {
    int core = sched_getcpu();
    cout << "Multiplication operation is running on core: " << core << endl;

    // Perform multiplication
    long long product = 1LL; // Initialize product as long long
    for (int num : array) {
        product *= num;
    }
    cout << "Product of the array elements: " << product << endl;
}

int main() {
    // Create two arrays
    vector<int> array1(ARRAY_SIZE, 1); // All elements initialized to 1
    vector<int> array2(ARRAY_SIZE, 2); // All elements initialized to 2

    // Create threads for addition and multiplication operations
    thread additionThread(additionOperation, ref(array1));
    thread multiplicationThread(multiplicationOperation, ref(array2));

    // Join the threads
    additionThread.join();
    multiplicationThread.join();

    return 0;
}
CODE 3
#include <iostream>
#include <thread>

using namespace std;

// Function to add seven to a number
void addSeven(long x, long* result) {
    *result = x + 7;
}

// Function to add two numbers
void addTwoNumbers(long x, long y, long* sum) {
    *sum = x + y;
}

// Function to add three numbers
void addThreeNumbers(long x, long y, long z, long* sum) {
    *sum = x + y + z;
}

int main() {
    // Initialize variables
    long a = 10;
    long b = 15, c = 20;
    long x = 2, y = 5, z = 30;
    
    long result1, result2, result3;

    // Create threads to perform operations
    thread th1(addSeven, a, &result1);
    thread th2(addTwoNumbers, b, c, &result2);
    thread th3(addThreeNumbers, x, y, z, &result3);

    // Wait for threads to finish
    th1.join();
    th2.join();
    th3.join();

    // Display results
    cout << result1 << " " << result2 << " " << result3 << endl;

    return 0;
}
CODE 4 
#include <iostream>
#include <thread>

void addSeven(long x, long* result) {
    *result = x + 7;
}

void addTwoNumbers(long x, long y, long* sum) {
    *sum = x + y;
}

void addThreeNumbers(long x, long y, long z, long* sum) {
    *sum = x + y + z;
}

int main() {
    long a = 10;
    long b = 15;
    long c = 20;
    long x = 2, y = 5, z = 30;

    long result1, result2, result3;

    std::thread th1(addSeven, a, &result1);
    std::thread th2(addTwoNumbers, b, c, &result2);
    std::thread th3(addThreeNumbers, x, y, z, &result3);

    th1.join();
    th2.join();
    th3.join();

    std::cout << result1 << " " << result2 << " " << result3 << "\n";

    return 0;
}
Code5
#include <iostream>
#include <thread>

void printMessage(long i) {
    std::cout << "I am thread " << i << "\n";
}

int main() {
    long numThreads = 5;
    std::thread allTh[numThreads];

    for (long i = 0; i < numThreads; ++i) {
        allTh[i] = std::thread(printMessage, i);
    }

    for (long i = 0; i < numThreads; ++i) {
        allTh[i].join();
    }

    return 0;
}
Code 6
#include <iostream>
#include <thread>

void memoryRace(long* aR) {
    *aR = 0;
    for (long i = 0; i < 100000; ++i) {
        *aR = *aR + 1;
    }
}

int main() {
    std::thread thArray[5];
    long result = 0;

    for (long i = 0; i < 5; ++i) {
        thArray[i] = std::thread(memoryRace, &result);
    }

    for (long i = 0; i < 5; ++i) {
        thArray[i].join();
    }

    std::cout << result << std::endl;

    return 0;
}
Code 7
#include <chrono>

class SimpleTimer {
private:
    int64_t timerStart;
    int64_t timerEnd;
    int64_t timerInProgress;

public:
    SimpleTimer(const int64_t& = 0);
    void start();
    void end();
    double getTime();
};

SimpleTimer::SimpleTimer(const int64_t& _inProgress) {
    timerInProgress = _inProgress;
    if (timerInProgress == 1) {
        timerStart = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    }
}

void SimpleTimer::start() {
    timerInProgress = 1;
    timerStart = std::chrono::high_resolution_clock::now().time_since_epoch().count();
}

void SimpleTimer::end() {
    timerInProgress = 2;
    timerEnd = std::chrono::high_resolution_clock::now().time_since_epoch().count();
}

double SimpleTimer::getTime() {
    if (timerInProgress != 2)
        return -1.0;

    double fR = double(timerEnd) - double(timerStart);
    fR /= 1000000000.0;
    return fR;
}
Code 8
#include <iostream>
#include "SimpleTimer.cpp"

long powerFunction(long a, long b, long p) {
    long res = 1;
    long i = 0;
    while (i < b) {
        res *= a;
        res %= p;
        i++;
    }
    return res;
}

void nonParallelFunction(long* alpha, long* beta, long* gamma, long p, long n) {
    long i = 0;
    while (i < n) {
        gamma[i] = powerFunction(alpha[i], beta[i], p);
        i++;
    }
}

int main() {
    std::cout << "Starting execution...\n";

    long *alpha, *beta, *gamma;
    long p = 17;
    long N = 100000;

    alpha = new long[N];
    beta = new long[N];
    gamma = new long[N];

    alpha[0] = 9;
    beta[0] = 5183;
    alpha[1] = 9;
    beta[1] = 4351;
    alpha[2] = 4711;

    long i = 3;
    while (i < N) {
        alpha[i] = alpha[i - 3];
        beta[i] = beta[i - 3];
        i++;
    }

    SimpleTimer tm;
    tm.start();
    nonParallelFunction(alpha, beta, gamma, p, N);
    tm.end();

    std::cout << "The non-parallel function running time is: " << tm.getTime() << "\n";
    std::cout << "First few terms of the sequence are:\n";

    for (i = 0; i < 100; i++) {
        std::cout << gamma[i] << "\t";
        if (i % 10 == 9)
            std::cout << "\n";
    }

    std::cout << "\n";
    delete[] alpha;
    delete[] beta;
    delete[] gamma;

    return 0;
}
Code 9
#include <iostream>
#include <thread>
#include "SimpleTimer.cpp"

long powerFunction(long a, long b, long p) {
    long res = 1;
    long i = 0;
    while (i < b) {
        res *= a;
        res %= p;
        i++;
    }
    return res;
}

void executeOneThread(long* alpha, long* beta, long* gamma, long p, long n, long start, long end) {
    while (start < end) {
        gamma[start] = powerFunction(alpha[start], beta[start], p);
        start++;
    }
}

void parallelFunction(long* alpha, long* beta, long* gamma, long p, long n, long t) {
    std::thread* allTh = new std::thread[t];

    long step = n / t;
    for (long i = 0; i < t; ++i) {
        allTh[i] = std::thread(executeOneThread, alpha, beta, gamma, p, n, i * step, (i + 1) * step);
    }

    for (long i = 0; i < t; ++i) {
        allTh[i].join();
    }

    delete[] allTh;
}

int main() {
    long numThreads = std::thread::hardware_concurrency();
    std::cout << "Starting execution...\n";

    long *alpha, *beta, *gamma;
    long p = 17;
    long N = 100000;

    alpha = new long[N];
    beta = new long[N];
    gamma = new long[N];

    alpha[0] = 9;
    beta[0] = 5183;
    alpha[1] = 9;
    beta[1] = 4351;
    alpha[2] = 4711;

    long i = 3;
    while (i < N) {
        alpha[i] = alpha[i - 3];
        beta[i] = beta[i - 3];
        i++;
    }

    SimpleTimer tm;
    tm.start();
    parallelFunction(alpha, beta, gamma, p, N, numThreads);
    tm.end();

    std::cout << "The parallel function running time is: " << tm.getTime() << "\n";
    std::cout << "First few terms of the sequence are:\n";

    for (i = 0; i < 100; i++) {
        std::cout << gamma[i] << "\t";
        if (i % 10 == 9)
            std::cout << "\n";
    }

    std::cout << "\n";
    delete[] alpha;
    delete[] beta;
    delete[] gamma;

    return 0;
}
Editor is loading...
Leave a Comment