[ create a new paste ] login | about

Link: http://codepad.org/T5jIUFPG    [ raw code | output | fork ]

C++, pasted on Oct 21:
#include <cstdio>
 #include <ctime>
 #include <algorithm>
 #include <functional>
 #include <windows.h>
 #include <omp.h>
 using namespace std;
 
 #define MAX_SEG_LEN		(20)
 
 int array [10000000] = {0};
 
 template <class T, int M>
 void init(T (&n)[M])
 {
 	for (int i = 0; i < M; ++i) n[i] = i;
 }
 
 template <class T, int M>
 void print(T (&n)[M])
 {
 	for (int i = 0; i < M; ++i) printf("%d ", n[i]);
 	printf("\n");
 }
 
 template <class T, int M>
 void disarrange(T (&n)[M])
 {
 	int len = rand() % (MAX_SEG_LEN - 2) + 2;
 	for (int start = 0; start < M; )
 	{
 		random_shuffle(n + start, n + (start + len > M ? M : start + len));
 		start += len;
 		len = rand() % (MAX_SEG_LEN - 2) + 2;
 	}
 }
 
 template <class T, int M>
 void sort(T (&n)[M])
 {
 	greater<T> greaterFnObj;
 #pragma omp parallel for
 	for (int i = 0; i < M - MAX_SEG_LEN; ++i)
 	{
 		make_heap(n + i, n + i + MAX_SEG_LEN + 1, greaterFnObj);
 	}
 	make_heap(n + M - MAX_SEG_LEN - 1, n + M);
 	sort_heap(n + M - MAX_SEG_LEN - 1, n + M);
 }
 
 int main(void)
 {
 	srand((unsigned int)time(NULL));
 	init(array);
 	// print(array);
 
 	disarrange(array);
 	// print(array);
 
 	omp_set_num_threads(2);
 
 	LARGE_INTEGER start, end, freq;
 	QueryPerformanceFrequency(&freq);
 	QueryPerformanceCounter(&start);
 	sort(array);
 	QueryPerformanceCounter(&end);
 	// print(array);
 	printf("Time cost : %.2lfms\n", (double)(end.QuadPart - start.QuadPart) / freq.QuadPart * 1000);
 
 	system("pause");
 	return 0;
 }
 


Output:
1
2
3
4
5
6
7
Line 21: error: windows.h: No such file or directory
Line 17: error: omp.h: No such file or directory
cc1plus: warnings being treated as errors
Line 42: warning: ignoring #pragma omp parallel
In function 'int main()':
Line 60: error: 'omp_set_num_threads' was not declared in this scope
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: