如何解决适用于Quicksort的OpenCL和MPI
我正在尝试使用OpenCL进行快速排序。任务是使用MPI将任务拆分到节点,然后在节点上使用OpenCL进行快速排序。我已经写了一个内核文件。但是,如果输入是
,我要么得到一个未排序的数组输入:22,1,58,77,98,5
输出是
输出1:1、22、58、77、98、5
或其中任意一位数字将重复并替换数组中的其他数字。
发布openCL内核文件
int partition(__global int *v,int start,int end) {
int pivotValue = v[end];
int pivotIndex = start;
for(int i = start; i < end; i++) {
if(v[i] < pivotValue) {
int temp= v[i];
v[i]=v[pivotIndex];
v[pivotIndex]=temp;
pivotIndex++;
}
}
int temp= v[pivotIndex];
v[pivotIndex]=v[end];
v[end]=temp;
return pivotIndex;
}
__kernel void normalsortArray(__global int* arr,__global int* stack,int l,int h) {
int top = -1;
// push initial values of l and h to stack
stack[++top] = l;
stack[++top] = h;
// Keep popping from stack while is not empty
while (top >= 0) {
// Pop h and l
h = stack[top--];
l = stack[top--];
// Set pivot element at its correct position
// in sorted array
int p = partition(arr,l,h);
// If there are elements on left side of pivot,// then push left side to stack
if (p - 1 > l) {
stack[++top] = l;
stack[++top] = p - 1;
}
// If there are elements on right side of pivot,// then push right side to stack
if (p + 1 < h) {
stack[++top] = p + 1;
stack[++top] = h;
}
}
}
__kernel void quickSortArray(const int n,__global int* arr,__global int* arrRes) {
// Thread identifiers
const int quantity = get_global_id(0);
normalsortArray(arr,arrRes,n-1);
for(int i = 0; i < n; i++)
{ arrRes[i] = arr[i];
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。