C++ Cuda c++;奇偶排序实现
这是我的奇偶排序代码: 这段代码正在编译,运行正常,但我猜不是排序。请帮帮我 我目前正在visual studio 2019上使用CUDA 11.3。 我的想法是创建奇偶函数并一个接一个地运行它。 我正在对每个函数的进程进行多线程处理。换句话说,如果(arr[2k]>arr[2k+1])交换(arr[2k],arr[2k+1])C++ Cuda c++;奇偶排序实现,c++,sorting,cuda,C++,Sorting,Cuda,这是我的奇偶排序代码: 这段代码正在编译,运行正常,但我猜不是排序。请帮帮我 我目前正在visual studio 2019上使用CUDA 11.3。 我的想法是创建奇偶函数并一个接一个地运行它。 我正在对每个函数的进程进行多线程处理。换句话说,如果(arr[2k]>arr[2k+1])交换(arr[2k],arr[2k+1]) #包括 #包括 #包括 #包括 #包括“设备启动参数.h” 使用名称空间std; 使用名称空间std::chrono; __全局无效偶数(int*arr,int n){
#包括
#包括
#包括
#包括
#包括“设备启动参数.h”
使用名称空间std;
使用名称空间std::chrono;
__全局无效偶数(int*arr,int n){
int index=threadIdx.x;
指数=指数*2;
如果(索引arr[index+1]){
int temp=arr[索引];
arr[index]=arr[index+1];
arr[指数+1]=温度;
}
}
}
__全局无效奇数(int*arr,int n){
int index=threadIdx.x;
指数=指数*2+1;
if(索引arr[索引+1]){
int temp=arr[索引];
arr[index]=arr[index+1];
arr[指数+1]=温度;
}
}
}
#定义n10
int main(){
int*a;
int*ptr;
常数int Size=sizeof(int)*n;
Cudamaloc((空隙**)和ptr,尺寸);
a=(int*)malloc(n*大小);
srand(时间(空));
对于(inti=0;i我怀疑有两个问题
首先,每次运行Odd()
时都会覆盖数组中的第一个值。要解决此问题,应该删除行arr[0]=0;
其次,您正在将主机指针a
而不是设备指针ptr
传递给内核。您应该传递ptr
通过这些(未经测试的)编辑,代码如下所示:
#include <stdio.h>
#include<iostream>
#include<chrono>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
using namespace std;
using namespace std::chrono;
__global__ void Even(int *arr, int n) {
int index = threadIdx.x;
index = index * 2;
if (index < n-1) {
if (arr[index ] > arr[index + 1]) {
int temp = arr[index];
arr[index] = arr[index+ 1];
arr[index + 1] = temp;
}
}
}
__global__ void Odd(int* arr, int n) {
int index = threadIdx.x;
index = index * 2+1;
// no longer setting a[0] = 0
if (index <= n - 2) {
if (arr[index ] > arr[index + 1]) {
int temp = arr[index];
arr[index] = arr[index + 1];
arr[index+ 1] = temp;
}
}
}
#define n 10
int main(){
int *a;
int* ptr;
const int Size = sizeof(int) * n;
cudaMalloc((void**)&ptr, Size);
a = (int*)malloc(n * Size);
srand(time(NULL));
for(int i =0 ;i<n;i++){
a[i] = rand()%n;
}
for (int i = 0; i < n; i++) {
std:: cout << a[i] << " ";
}
std::cout << endl;
cudaMemcpy(ptr, a, Size, cudaMemcpyHostToDevice);
auto starttime = high_resolution_clock::now();
for (int i = 0; i < n / 2; i++) {
Even<<<1,n >>>(ptr, n); // ptr instead of a
Odd<<<1,n >>>(ptr, n); // ptr instead of a
}
cudaMemcpy( a, ptr, Size, cudaMemcpyDeviceToHost);
auto stoptime = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stoptime-starttime);
std::cout<<" time : " <<duration.count()<<"ms"<<endl;
for (int i = 0; i < n; i++) {
std::cout << a[i] << " ";
}
std::cout << endl;
free(a);
cudaFree(ptr);
return 0;
}
#包括
#包括
#包括
#包括
#包括“设备启动参数.h”
使用名称空间std;
使用名称空间std::chrono;
__全局无效偶数(int*arr,int n){
int index=threadIdx.x;
指数=指数*2;
如果(索引arr[index+1]){
int temp=arr[索引];
arr[index]=arr[index+1];
arr[指数+1]=温度;
}
}
}
__全局无效奇数(int*arr,int n){
int index=threadIdx.x;
指数=指数*2+1;
//不再设置[0]=0
if(索引arr[索引+1]){
int temp=arr[索引];
arr[index]=arr[index+1];
arr[指数+1]=温度;
}
}
}
#定义n10
int main(){
int*a;
int*ptr;
常数int Size=sizeof(int)*n;
Cudamaloc((空隙**)和ptr,尺寸);
a=(int*)malloc(n*大小);
srand(时间(空));
for(int i=0;iI可能忽略了一些内容,但是为什么要在Odd()
函数中设置arr[0]=0;
?这不是覆盖了数组中第一个值的内容吗?除了我在前面的评论中提到的问题之外,看起来您正在使用a
调用内核(主机指针)而不是使用ptr
(设备指针)。当我运行您的代码并将内核调用更改为使用ptr
而不是a
时,我会得到一个排序结果。您可能有一个损坏的CUDA安装。除了在两个内核调用上都从a
更改为ptr
,我建议添加。感谢您回答我的问题。我刚刚按照您告诉我的方式进行了测试,但这很简单同一个数组没有排序。目前,我正在AWS windows上运行我的代码。可能是问题吗?只是为了确定,你确定你重新编译了新代码吗?我知道我有时会忘记自己这么做。是的,我重新编译了新代码。我甚至尝试了5,6次。同一个未排序的数组。谢谢你的再次检查,我只是想排除这个问题。继续我注意到的另一个潜在问题是,您正在调用a=(int*)malloc(n*Size);
,但是Size
已经解释了n
。如果您将其更改为a=(int*)malloc(Size);
,它仍然是相同的未排序的打印数组。非常感谢您检查我的代码并与我分享您的意见
#include <stdio.h>
#include<iostream>
#include<chrono>
#include <cuda_runtime.h>
#include "device_launch_parameters.h"
using namespace std;
using namespace std::chrono;
__global__ void Even(int *arr, int n) {
int index = threadIdx.x;
index = index * 2;
if (index < n-1) {
if (arr[index ] > arr[index + 1]) {
int temp = arr[index];
arr[index] = arr[index+ 1];
arr[index + 1] = temp;
}
}
}
__global__ void Odd(int* arr, int n) {
int index = threadIdx.x;
index = index * 2+1;
// no longer setting a[0] = 0
if (index <= n - 2) {
if (arr[index ] > arr[index + 1]) {
int temp = arr[index];
arr[index] = arr[index + 1];
arr[index+ 1] = temp;
}
}
}
#define n 10
int main(){
int *a;
int* ptr;
const int Size = sizeof(int) * n;
cudaMalloc((void**)&ptr, Size);
a = (int*)malloc(n * Size);
srand(time(NULL));
for(int i =0 ;i<n;i++){
a[i] = rand()%n;
}
for (int i = 0; i < n; i++) {
std:: cout << a[i] << " ";
}
std::cout << endl;
cudaMemcpy(ptr, a, Size, cudaMemcpyHostToDevice);
auto starttime = high_resolution_clock::now();
for (int i = 0; i < n / 2; i++) {
Even<<<1,n >>>(ptr, n); // ptr instead of a
Odd<<<1,n >>>(ptr, n); // ptr instead of a
}
cudaMemcpy( a, ptr, Size, cudaMemcpyDeviceToHost);
auto stoptime = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stoptime-starttime);
std::cout<<" time : " <<duration.count()<<"ms"<<endl;
for (int i = 0; i < n; i++) {
std::cout << a[i] << " ";
}
std::cout << endl;
free(a);
cudaFree(ptr);
return 0;
}