Skip to content

Instantly share code, notes, and snippets.

@vadim8kiselev
Created October 20, 2017 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadim8kiselev/a02402003cea4576ed31103a7e9c2858 to your computer and use it in GitHub Desktop.
Save vadim8kiselev/a02402003cea4576ed31103a7e9c2858 to your computer and use it in GitHub Desktop.
//My_Task_A.cpp
#include "My_Task.h"
#include <math.h>
using namespace std;
double function(int x)
{
double result = 0;
for (int k = 1; k < fmax(20, 20 * fabs(x)); k++)
{
for (int j = 1; j < fmax(20, 20 * fabs(x)); j++)
{
double leftPart = (((x * x + x) * (k - j)) + .0)
/ (x * x + k * k * k + j * j * j);
double rightPart = sin(k * x) * cos(j * x);
result += leftPart * rightPart;
}
}
return 1;
}
// My_Task.h
double function(int k);
//parallel_for.cpp
#include <locale.h>
#include <time.h>
#include <ppl.h>
#include <iostream>
#include <vector>
#include "My_Task.h"
#define N 200
void task1();
void task1_2();
void task2();
int main(int argc, char *argv[])
{
setlocale(LC_ALL, ".ACP");
//task1();
//task1_2();
task2();
}
void task1() {
double V[N];
double Time = clock();
for (int k = 0; k < N; k++) {
V[k] = function(k);
}
Time = (clock() - Time) / CLOCKS_PER_SEC;
std::cout << "Подзадачи завершены" << std::endl << "Время посл. обр.: " << Time << " сек." << std::endl;
Time = clock();
Concurrency::parallel_for(0, N, [&V](int k) {V[k] = function(k); });
Time = (clock() - Time) / CLOCKS_PER_SEC;
std::cout << "Подзадачи завершены" << std::endl << "Время парал. обр.: " << Time << " сек." << std::endl;
}
void task1_2() {
std::vector<double> v(N);
/* Initialization */
for (int index = 0; index < N; index++)
{
v[index] = 100 * cos(index);
}
double Time = clock();
std::for_each(v.begin(), v.end(), [&v](int k) {v[k] = function(v[k]); });
Time = (clock() - Time) / CLOCKS_PER_SEC;
std::cout << "Подзадачи завершены" << std::endl << "Время посл. обр.: " << Time << " сек." << std::endl;
Time = clock();
Concurrency::parallel_for_each(v.begin(), v.end(), [&v](int k) {v[k] = function(v[k]); });
Time = (clock() - Time) / CLOCKS_PER_SEC;
std::cout << "Подзадачи завершены" << std::endl << "Время парал. обр.: " << Time << " сек." << std::endl;
}
void task2() {
std::vector<double> v(N);
/* Initialization */
for (int index = 0; index < N; index++)
{
v[index] = 100 * cos(index);
}
double Time = clock();
std::transform(v.begin(), v.end(), std::back_inserter(v), [](int k) {return function(k); });
Time = (clock() - Time) / CLOCKS_PER_SEC;
std::cout << "Подзадачи завершены" << std::endl << "Время посл. обр.: " << Time << " сек." << std::endl;
Time = clock();
Concurrency::parallel_transform(v.begin(), v.end(), std::back_inserter(v), [](int k) {return function(k); });
Time = (clock() - Time) / CLOCKS_PER_SEC;
std::cout << "Подзадачи завершены" << std::endl << "Время парал. обр.: " << Time << " сек." << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment