Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <fstream>
- #include <vector>
- #include <time.h>
- #include < sstream>
- using namespace std;
- struct myData {
- int a;
- int b;
- double eps;
- double delta;
- };
- int allThreadsTasks = 0;
- int noSolutions = 0;
- int mystakeTasks = 0;
- vector <myData> parametrs;
- vector <pair<myData, string>> results;
- CRITICAL_SECTION cs1;
- CRITICAL_SECTION cs2;
- CRITICAL_SECTION cs3;
- double f(double a, double b, double x) {
- return a*cos(x) / (b*x*x);
- }
- double df(double a, double b, double x) {
- return -(cos(x)*a * 2 / (b * x*x*x) + a*sin(x) / (b*x*x));
- }
- double d2f(double a, double b, double x) {
- return a*(-cos(x) / b + 4 * sin(x) / (3 * x) + 2 * cos(x) / (x*x)) / (x*x);
- }
- bool CheckSolutions(double a, double b, double i1, double i2) {
- if (f(a, b, i1)*f(a, b, i2) > 0) {
- if (abs(i1 - i2) < 0.001) {
- return false;
- }
- double c = (i1 + i2) / 2;
- CheckSolutions(a, b, i1, c);
- CheckSolutions(a, b, c, i2);
- }
- else {
- return true;
- }
- }
- class Thread {
- public:
- HANDLE tr;
- int ID=0;
- int tasks = 0;
- double maxTime=0;
- double minTime=100;
- double allTime=0;
- Thread() {}
- int rez(myData data) {
- double start = clock();
- double i1 = 0, i2 = 100;
- double xk = data.delta;
- double xk_1 = 0;
- string str;
- pair <myData, string> p;
- p.first = data;
- bool flag = true;
- if (data.b == 0 || data.delta == 0 ) {
- noSolutions++;
- p.second = "No solutions";
- flag = false;
- }
- if (!CheckSolutions(data.a, data.b, i1, i2) && flag) {
- flag = false;
- p.second = "No solutions";
- noSolutions++;
- }
- while (abs(xk - xk_1) > data.eps && flag) {
- xk_1 = xk;
- if (df(data.a, data.b, xk_1) == 0) {
- p.second = "No solutions";
- noSolutions++;
- flag = false;
- break;
- }
- xk = xk_1 - (f(data.a, data.b, xk_1) / df(data.a, data.b, xk_1));
- }
- if (flag) {
- stringstream ss;
- ss << xk;
- ss >> p.second;
- EnterCriticalSection(&cs3);
- allThreadsTasks++;
- LeaveCriticalSection(&cs3);
- tasks++;
- }
- double end = clock();
- double time = (double)((end - start) / (double)CLOCKS_PER_SEC);
- if (maxTime < time) {
- maxTime = time;
- }
- if (minTime > time) {
- minTime = time;
- }
- allTime += time;
- EnterCriticalSection(&cs2);
- results.push_back(p);
- LeaveCriticalSection(&cs2);
- return 0;
- }
- };
- vector <Thread> threads;
- DWORD WINAPI resolve(LPVOID i) {
- while (true) {
- bool flag = false;
- myData data;
- if (parametrs.size() == 0) {
- break;
- }
- EnterCriticalSection(&cs1);
- if (parametrs.size() != 0) {
- data = parametrs[0];
- parametrs.erase(parametrs.begin());
- flag = true;
- }
- LeaveCriticalSection(&cs1);
- if (flag) {
- threads[(int)i].rez(data);
- }
- }
- return 0;
- }
- int main() {
- srand(time(NULL));
- setlocale(LC_ALL, ".1251");
- InitializeCriticalSection(&cs1);
- InitializeCriticalSection(&cs2);
- InitializeCriticalSection(&cs3);
- int TestCount, ThreadCount;
- cout << "TestCount = ";
- cin >> TestCount;
- cout << endl<<"ThreadCount = ";
- cin >> ThreadCount;
- cout << endl;
- DWORD IDThread;
- for (int i = 0; i < TestCount; i++) {
- double a = rand() % 100;
- double b = rand() % 100;
- double eps = (rand() % 100) / 100000.0;
- double delta = (rand() % 10000) / 100;
- myData data;
- data.a = a;
- data.b = b;
- data.eps = eps;
- data.delta = delta;
- parametrs.push_back(data);
- }
- for (int i = 0; i < ThreadCount; i++) {
- Thread a;
- threads.push_back(a);
- threads[i].ID = i;
- }
- HANDLE *handles = new HANDLE[ThreadCount];
- int i = 0;
- while (i<ThreadCount ) {
- handles[i] = threads[i].tr = CreateThread(NULL, 0, resolve, (void*)threads[i].ID, 0, &IDThread);
- i++;
- }
- WaitForMultipleObjects(ThreadCount, handles, TRUE, INFINITE);
- DeleteCriticalSection(&cs1);
- DeleteCriticalSection(&cs2);
- DeleteCriticalSection(&cs3);
- cout << "Всего решено задач: " << allThreadsTasks << endl;
- cout << "Не решилось из-за ошибки в процессе вычисления: " << mystakeTasks << endl;
- cout << "Не имеют решений: " << noSolutions << endl;
- cout << "-------------------------------------------------------------------------------------------------------------------------------------------" << endl;
- for (int i = 0; i < ThreadCount; i++) {
- cout << "Поток номер " << threads[i].ID << " | Kол-во решенных задач: " << threads[i].tasks << " \t| Время решения всех задач: ";
- cout << threads[i].allTime << " | Макс. время решения: " << threads[i].maxTime << " мил-сек | Мин. время решения задачи: " << threads[i].minTime <<" мил-сек"<< endl;
- }
- ofstream out("output.txt");
- double start = clock();
- for (int i = 0; i < results.size(); i++){
- out << "a=" << results[i].first.a;
- out << " b=" << results[i].first.b;
- out << " eps=" << results[i].first.eps;
- out << " rez=" << results[i].second.c_str() << endl;
- }
- double end = clock();
- cout << "Время затраченное на запись данных в файл " << (double)((end - start)/(double)CLOCKS_PER_SEC) << " миллисекунд" << endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement