Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <thread>
- #include <vector>
- #include <iostream>
- #include <chrono>
- using namespace std;
- int threadsNumber = 8;
- double t = 0.01;
- int iterationCount = 10000000;
- double fRand(double fMin, double fMax) {
- double f = (double)rand() / RAND_MAX;
- return fMin + f * (fMax - fMin);
- }
- void calculatePointsOfFigure (int tid, double a, double b, double c) {
- int xd = sched_getcpu()+1;
- double x = 0.1;
- double y = 0.1;
- double z = 0.1;
- tid++;
- for (int i = 0; i < iterationCount; i++ ) {
- double xt = x + t * a * (y - x);
- double yt = y + t * (x * (b - z) - y);
- double zt = z + t * (x * y - c * z);
- x = xt;
- y = yt;
- z = zt;
- }
- printf("Thread #%d, started on core #%d, executed on core #%d\n", tid, xd, sched_getcpu()+1);
- printf("x = %f\n", x);
- printf("y = %f\n", y);
- printf("z = %f\n", z);
- printf("\n");
- }
- int main() {
- typedef chrono::high_resolution_clock Time;
- typedef chrono::milliseconds ms;
- typedef chrono::duration<float> fsec;
- unsigned numCPU = thread::hardware_concurrency();
- cout << "Number of cores on this machine is " << numCPU << endl;
- vector<thread> threads;
- auto begin = Time::now();
- for (int i = 0; i < threadsNumber; i++) {
- double randomA = fRand(5.0, 20.0);
- double randomB = fRand(20.0, 35.4);
- double randomC = fRand(1.0, 3.7);
- threads.emplace_back(thread(calculatePointsOfFigure, i, randomA, randomB, randomC));
- }
- for (auto& th : threads)
- th.join();
- auto end = Time::now();
- fsec fs = end - begin;
- cout << fs.count() << " second" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement