Ich habe auch eine Klasse auf der Hostseite, die std::allocator verwendet. Es funktioniert gut. Aber dieser gibt einen Segmentierungsfehler.
Ich bin mir nicht sicher, was hier falsch ist. Wie kann ich Thrust Lib verwenden, um eine einfache Klasse mit Gerätezeiger und Füllkonstruktor zu haben?
Der einfache Code unten gibt mir nur Folgendes:
Segmentierungsfehler
Die Ausführung ist mit einem Exit-Code ungleich Null fehlgeschlagen: 139
Code: Select all
#include
#include
#include // Provides thrust::device_allocator
#include
#include
// The default allocator now uses the correct, fully qualified name:
// thrust::device_allocator
template
class flat1d {
public:
using value_type = T;
using size_type = std::size_t;
using allocator_type = Allocator;
// Use std::allocator_traits to get the correct Thrust pointer type
using pointer = typename std::allocator_traits::pointer;
private:
pointer data_ = nullptr;
size_type size_ = 0;
allocator_type alloc_;
public:
// Destructor: Cleans up the device memory using the allocator
~flat1d() {
if (data_ != nullptr) {
alloc_.deallocate(data_, size_);
}
}
flat1d() = default;
// The Fill Constructor
flat1d(size_type count, const value_type& value, const Allocator& alloc = Allocator())
: size_(count), alloc_(alloc)
{
if (count == 0) return;
// 1. Allocate device memory
data_ = alloc_.allocate(count);
// 2. Use Thrust fill algorithm
thrust::fill(data_, data_ + size_, value);
}
};
// --- Example Usage (Main function) ---
int main() {
const std::size_t N = 10;
const float FILL_VALUE = 3.14159f;
std::cout
Mobile version