Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <process.h>
  6. #include <time.h>
  7. #include <random>
  8.  
  9. using namespace std;
  10.  
  11. const int threads = 4;
  12.  
  13. int n = 0;
  14. int k = 0;
  15. int amount = 0;
  16.  
  17. default_random_engine generator;
  18. uniform_int_distribution<int> distribution(0, 32767);
  19.  
  20. //ofstream out("functions.txt");
  21.  
  22. //CRITICAL_SECTION cs;
  23.  
  24. unsigned int WINAPI ThreadFunction(LPVOID pvParam)
  25. {
  26.     // выделение памяти
  27.     int* coeffs = new int[n];           // коэффициенты функции
  28.     int* borders = new int[k - 1];      // пороговые значения
  29.     int* vars = new int[n];             // промежуточные значения переменных
  30.     int* values = new int[pow(k, n)];   // столбец значений
  31.     int thread_id = (int)pvParam;
  32.     int index;
  33.     int linear;
  34.     int min;
  35.     int max;
  36.     string filename = "functions";
  37.     filename += (thread_id + '0');
  38.     filename += ".txt";
  39.     ofstream thread_out(filename);
  40.     // инициализация коэффициентов функции
  41.     while (amount > 0)
  42.     {
  43.         amount--;
  44.         min = 0;
  45.         max = 0;
  46.         for (int i = 0; i < n; i++)
  47.         {
  48.             coeffs[i] = (distribution(generator) % 20000) - 10000;
  49.             if (coeffs[i] < 0)
  50.                 min += (coeffs[i] * (k - 1));
  51.             else if (coeffs[i] > 0)
  52.                 max += (coeffs[i] * (k - 1));
  53.         }
  54.         // инициализация порогов функции
  55.         for (int i = 0; i < k - 1; i++)
  56.         {
  57.             borders[i] = (distribution(generator) % (max - min)) + min;
  58.             for (int j = 0; j < i; j++)
  59.             {
  60.                 while (borders[i] == borders[j])
  61.                     borders[i] = (distribution(generator) % (max - min)) + min;
  62.                 if (borders[j] > borders[i])
  63.                     swap(borders[i], borders[j]);
  64.             }
  65.         }
  66.         // вычисление столбца значений
  67.         for (int i = 0; i < pow(k, n); i++)
  68.         {
  69.             index = i;
  70.             linear = 0;
  71.             for (int j = n - 1; j >= 0; j--)
  72.             {
  73.                 vars[j] = index % k;
  74.                 linear += (vars[j] * coeffs[j]);
  75.                 index -= vars[j];
  76.                 index /= k;
  77.             }
  78.             values[i] = k - 1;
  79.             for (int j = 0; j < k - 1; j++)
  80.                 if (linear < borders[j])
  81.                 {
  82.                     values[i] = j;
  83.                     break;
  84.                 }
  85.             thread_out << values[i] << " ";
  86.         }
  87.         thread_out << "\n";
  88.         //EnterCriticalSection(&cs);
  89.         /*for (int i = 0; i < pow(k, n); i++)
  90.             thread_out << values[i] << " ";
  91.         thread_out << "\n";*/
  92.         //amount--;
  93.         //LeaveCriticalSection(&cs);
  94.     }
  95.     delete[] coeffs;
  96.     delete[] borders;
  97.     delete[] vars;
  98.     delete[] values;
  99.     thread_out.close();
  100.     return 0;
  101. }
  102.  
  103. void initialize()
  104. {
  105.     cout << "n = ";
  106.     cin >> n;
  107.     cout << "k = ";
  108.     cin >> k;
  109.     cout << "amount = ";
  110.     cin >> amount;
  111.     return;
  112. }
  113.  
  114. void writeInfo()
  115. {
  116.     ofstream out("info.txt");
  117.     out << n << " " << k << " " << amount << " " << threads;
  118.     out.close();
  119. }
  120.  
  121. int main()
  122. {
  123.     srand(time(NULL));
  124.     int starttime = GetTickCount();
  125.     HANDLE hThreads[threads];
  126.     initialize();
  127.     writeInfo();
  128.     //InitializeCriticalSection(&cs);
  129.     for (int i = 0; i < threads; i++)
  130.     {
  131.         hThreads[i] = (HANDLE)_beginthreadex(NULL, 0, ThreadFunction, (LPVOID)i, 0, NULL);
  132.         if (hThreads[i] == NULL)
  133.         {
  134.             printf("[x] thread #%d cannot be created, error %d", i, GetLastError());
  135.             return 1;
  136.         }
  137.     }
  138.     WaitForMultipleObjects(threads, hThreads, TRUE, INFINITE);
  139.     //DeleteCriticalSection(&cs);
  140.     printf("program is finished in %d ms\n", (GetTickCount() - starttime));
  141.     system("pause");
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement