1. 程式人生 > >count_sort計數排序OpenMP的並行化

count_sort計數排序OpenMP的並行化

簡述

計數排序,就是統計某個數值在所有的數字中所應該存在的位置,然後,放到一個確定的位置上。非常簡單的排序演算法。

程式

  • 會讀取data.txt中的檔案
  • 資料的樣子
10 
0 2 3 1 4 8 6 7 5 9
  • 效果
PS D:\C++\VS\repo\OpenMP-TEST\Debug> ./OpenMP-TEST
0 1 2 3 4 5 6 7 8 9
  • 程式碼
#include <iostream>
#include <omp.h>
#include <fstream>
using namespace std; #pragma warning(disable : 4996) int main(int argc, char **argv) { if (argc < 1) return 0; ifstream cin("./data.txt"); int i, j, count, n; cin >> n; int *a = new int[n]; int *temp = new int[n]; //int thread_count = strtol(argv[1], NULL, 10); for (i = 0; i < n; ++
i) cin >> a[i]; #pragma omp parallel for private(i, j, count) shared(n, a, temp) for (i = 0; i < n; ++i) { count = 0; for (j = 0; j < n; ++j) { if (a[j] < a[i]) count++; else if (a[j] == a[i] && j < i) count++; } temp[count] = a[i]; } #pragma omp parallel for
for (i = 0; i < n; ++i) a[i] = temp[i]; for (i = 0; i < n; ++i) cout << a[i]<< " "; cout << endl; delete[]temp; delete[]a; }

和序列的計數排序進行對比

  • 不要輸出排序結果,因為資料量太大了。
  • 10000個數據的排序效果
PS D:\C++\VS\repo\OpenMP-TEST\Debug> ./OpenMP-TEST r
Total time Serial: 1.256s
Total time parallel: 0.141s
  • 接近9倍的速度(因為我在筆記本上有8個核)
  • 後面多了一個r命令,其實是免得在vs編譯的時候被呼叫。隨便寫什麼都是ok的。
#include <iostream>
#include <omp.h>
#include <fstream>
#include <ctime>
using namespace std;
#pragma warning(disable : 4996)
int main(int argc, char **argv) {
	if (argc < 2) return 0;
	ifstream cin("./data.txt");
	int i, j, count, n;
	cin >> n;
	int *a = new int[n];
	int *b = new int[n];
	int *temp = new int[n];
	
	//int thread_count = strtol(argv[1], NULL, 10);
	for (i = 0; i < n; ++i) cin >> b[i];
	for (i = 0; i < n; ++i) a[i] = b[i];
	
	clock_t starttime, endtime;
	starttime = clock();
	for (i = 0; i < n; ++i) {
		count = 0;
		for (j = 0; j < n; ++j) {
			if (b[j] < b[i]) count++;
			else if (b[j] == b[i] && j < i) count++;
		}
		temp[count] = b[i];
	}
	memcpy(b, temp, n * sizeof(int));
	endtime = clock();

	// for (i = 0; i < n; ++i) cout << b[i] << " ";
	// cout << endl;
	cout << "Total time Serial: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;

	starttime = clock();
#pragma omp parallel for private(i, j, count) shared(n, a, temp)
	for (i = 0; i < n; ++i) {
		count = 0;
		for (j = 0; j < n; ++j) {
			if (a[j] < a[i]) count++;
			else if (a[j] == a[i] && j < i) count++;
		}
		temp[count] = a[i];
	}

#pragma omp parallel for 
	for (i = 0; i < n; ++i) a[i] = temp[i];

	endtime = clock();

	//for (i = 0; i < n; ++i) cout << a[i]<< " ";
	//cout << endl;
	cout << "Total time parallel: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;
	delete[]temp;
	delete[]a;
	delete[]b;
}

和qsort的序列版也做對比

在這裡插入圖片描述

#include <iostream>
#include <omp.h>
#include <fstream>
#include <ctime>
using namespace std;
#pragma warning(disable : 4996)
int cmp(const void * a, const void *b) //from small to big
{
	return *(int *)a - *(int *)b;
}

int main(int argc, char **argv) {
	if (argc < 2) return 0;
	ifstream cin("./data.txt");
	int i, j, count, n;
	cin >> n;
	int *a = new int[n];
	int *b = new int[n];
	int *c = new int[n];
	int *temp = new int[n];
	
	//int thread_count = strtol(argv[1], NULL, 10);
	for (i = 0; i < n; ++i) cin >> b[i];
	for (i = 0; i < n; ++i) { a[i] = b[i]; c[i] = b[i]; }
	

	clock_t starttime, endtime;
	starttime = clock();
	qsort(c, n, sizeof(int), cmp);
	endtime = clock();
	cout << "Total time Serial-qsort: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;

	starttime = clock();
	for (i = 0; i < n; ++i) {
		count = 0;
		for (j = 0; j < n; ++j) {
			if (b[j] < b[i]) count++;
			else if (b[j] == b[i] && j < i) count++;
		}
		temp[count] = b[i];
	}
	memcpy(b, temp, n * sizeof(int));
	endtime = clock();
	cout << "Total time Serial: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;

	starttime = clock();
#pragma omp parallel for private(i, j, count) shared(n, a, temp)
	for (i = 0; i < n; ++i) {
		count = 0;
		for (j = 0; j < n; ++j) {
			if (a[j] < a[i]) count++;
			else if (a[j] == a[i] && j < i) count++;
		}
		temp[count] = a[i];
	}

#pragma omp parallel for 
	for (i = 0; i < n; ++i) a[i] = temp[i];

	endtime = clock();

	//for (i = 0; i < n; ++i) cout << a[i]<< " ";
	//cout << endl;
	cout << "Total time parallel: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;
	delete[]temp;
	delete[]a;
}