Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "omp.h"
- #include "stdio.h"
- #include <stdlib.h>
- #include <windows.h>
- #include <iostream>
- #include <cstdlib>
- #include <process.h>
- using namespace std;
- const long long n = 100000000;
- static short* tablica = new short[n];
- volatile DWORD licznik;
- long long int suma;
- CRITICAL_SECTION g_Section;
- long long suma1(short* tab, long long n) {
- long long sum = 0;
- long long i;
- for (i = 0; i < n; i++) {
- sum = sum + tab[i];
- }
- return sum;
- }
- unsigned int g_Counter = 0;
- void __cdecl ThreadProc(void * Args)
- {
- int j = n / 2;
- for (int i = 0; i < j; i++)
- {
- EnterCriticalSection(&g_Section);
- suma = suma + tablica[i];
- LeaveCriticalSection(&g_Section);
- }
- _endthread();
- }
- void __cdecl ThreadProc2(void * Args)
- {
- int j = n / 2;
- for (j; j < n; j++)
- {
- EnterCriticalSection(&g_Section);
- suma = suma + tablica[j];
- LeaveCriticalSection(&g_Section);
- }
- _endthread();
- }
- void __cdecl ThreadProc3(void * Args)
- {
- int j = n / 2;
- long long int sum = 0;
- for (int i = 0; i < j; i++)
- {
- sum = sum + tablica[i];
- }
- EnterCriticalSection(&g_Section);
- suma = suma + sum;
- LeaveCriticalSection(&g_Section);
- _endthread();
- }
- void __cdecl ThreadProc4(void * Args)
- {
- int j = n / 2;
- long long int sum = 0;
- for (j; j < n; j++)
- {
- sum = sum + tablica[j];
- }
- EnterCriticalSection(&g_Section);
- suma = suma + sum;
- LeaveCriticalSection(&g_Section);
- _endthread();
- }
- long long suma2(long long n) {
- long long i;
- suma = 0;
- HANDLE hThread = (HANDLE)_beginthread(ThreadProc, 0, NULL);
- HANDLE hThread2 = (HANDLE)_beginthread(ThreadProc2, 0, NULL);
- WaitForSingleObject(hThread, INFINITE);
- WaitForSingleObject(hThread2, INFINITE);
- return suma;
- }
- long long suma3(long long n) {
- long long i;
- suma = 0;
- HANDLE hThread3 = (HANDLE)_beginthread(ThreadProc3, 0, NULL);
- HANDLE hThread4 = (HANDLE)_beginthread(ThreadProc4, 0, NULL);
- WaitForSingleObject(hThread3, INFINITE);
- WaitForSingleObject(hThread4, INFINITE);
- return suma;
- }
- int main(int argc, char* argv[]) {
- InitializeCriticalSection(&g_Section);
- cout << "ilosc dostepnych rdzeni : " << omp_get_num_procs() << endl;
- long long i;
- for (int j = 0; j < 3; j++){
- licznik = GetTickCount();
- #pragma omp parallel for private(i) num_threads(4)
- for (i = 0; i < n; i++){
- tablica[i] = rand() % 10;
- }
- cout << "Czas tworzenia tablicy: ";
- cout << GetTickCount() - licznik << endl << endl;
- long long suma;
- for (int i = 0; i < 3; i++)
- {
- licznik = GetTickCount();
- suma = suma1(tablica, n);
- cout << "wersja iteracyjna" << endl;
- cout << "ilosc elementow: " << n << endl;
- cout << "suma: " << suma << endl;
- cout << "czas wykonania: " << GetTickCount() - licznik << endl;
- }
- printf("\n\n");
- for (int i = 0; i < 3; i++)
- {
- licznik = GetTickCount();
- suma = suma2(n);
- cout << "wersja na 2 watkach z sekcja krytyczna" << endl;
- cout << "ilosc elementow: " << n << endl;
- cout << "suma: " << suma << endl;
- cout << "czas wykonania: " << GetTickCount() - licznik << endl;
- }
- cout << endl << endl;
- for (int i = 0; i < 3; i++)
- {
- licznik = GetTickCount();
- suma = suma3(n);
- cout << "wersja na 2 watkach z zmienna lokalna i sekcja krytyczna" << endl;
- cout << "ilosc elementow: " << n << endl;
- cout << "suma: " << suma << endl;
- cout << "czas wykonania: " << GetTickCount() - licznik << endl;
- }
- system("pause");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement