简体   繁体   English

使用推力(素数)在GPU中编译

[英]compiling in GPU using thrust (prime numbers)

I have a code written in thrust in C language, but i am not able to compile it since i dont have a GPU. 我有一个用C语言编写的代码,但是由于没有GPU,所以无法编译它。 My code is supposed to calculate the first 1000 prime numbers. 我的代码应该计算前1000个素数。 That's it. 而已。 But the problem is 但是问题是

1 - i can not compile it since i do not have a GPU. 1-我没有GPU,因此无法编译。

2 - Since i can not compile it, i can not know if it really calculates prime numbers. 2-由于我无法编译它,所以我不知道它是否真的计算素数。

Here is my code: 这是我的代码:

`struct prime{
_host_ _device_
    void operator()(long& x){
    bool result = true;
    long stop = ceil(sqrt((float)x));
    if(x%2!=0){
        for(int i = 3;i<stop;i+=2){
            if(x%i==0){
                result = false;
                break;
            };
        }
    }else{
        result = false;
    }
    if(!result)
        x = -1;
 }
};
void doTest(long gen){
  using namespace thrust;
  device_vector<long> tNum(gen);
  thrust::sequence(tNum.begin(),tNum.end());
}
int main(){
   doTest(1000);
   return 0;
}`

Could someone help me compile my code and display the result, and if not working correctly, then help me fix it? 有人可以帮助我编译我的代码并显示结果,如果不能正常工作,则可以帮助我修复它吗?

If you don't have a GPU, then use thrust::host_vector instead of thrust::device_vector . 如果您没有GPU,请使用thrust::host_vector而不是thrust::device_vector

I've cleaned up your code, and it's running on the CPU like this: 我清理了您的代码,它正在CPU上运行,如下所示:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sequence.h>

#include <iostream>


int main()
{
    thrust::host_vector<long> tNum(1000);
    thrust::sequence(std::begin(tNum), std::end(tNum));
    thrust::transform(std::cbegin(tNum), std::cend(tNum), std::begin(tNum), [](long x)
    {
        bool result = true;
        long stop = (long)std::ceil(std::sqrt((float)x));
        if (x % 2 != 0) {
            for (long i = 3; i < stop; i += 2) {
                if (x % i == 0) {
                    result = false;
                    break;
                };
            }
        } else {
            result = false;
        }
        if (!result) x = -1;
        return x;
    });
    for (const auto& element : tNum) if (element>0) std::cout << element << ", ";
    std::cout << std::endl;

    std::cin.ignore();
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM