diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 0b9c62bdd..559a9cf80 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,24 +1,20 @@ + +# Input parameters: +# - File name of the example without file extension +# - File extension of the example macro(stdgpu_detail_add_example) set(STDGPU_EXAMPLES_NAME "${ARGV0}") add_executable(${STDGPU_EXAMPLES_NAME} "${STDGPU_EXAMPLES_NAME}.${ARGV1}") target_link_libraries(${STDGPU_EXAMPLES_NAME} PRIVATE stdgpu::stdgpu) endmacro() -macro(stdgpu_add_example_cu) - stdgpu_detail_add_example(${ARGV0} "cu") -endmacro() - macro(stdgpu_add_example_cpp) stdgpu_detail_add_example(${ARGV0} "cpp") endmacro() -stdgpu_add_example_cu(bitset) -stdgpu_add_example_cu(container_iterator) -stdgpu_add_example_cu(container_kernel) stdgpu_add_example_cpp(contract) -stdgpu_add_example_cu(createAndDestroyDeviceArray) -stdgpu_add_example_cu(createAndDestroyDeviceObject) -stdgpu_add_example_cu(mutex_array) -stdgpu_add_example_cu(thrust_interoperability) -stdgpu_add_example_cu(thrust_towards_ranges) +stdgpu_add_example_cpp(createAndDestroyDeviceArray) +stdgpu_add_example_cpp(createAndDestroyDeviceObject) + +add_subdirectory(cuda) diff --git a/examples/createAndDestroyDeviceArray.cu b/examples/createAndDestroyDeviceArray.cpp similarity index 100% rename from examples/createAndDestroyDeviceArray.cu rename to examples/createAndDestroyDeviceArray.cpp diff --git a/examples/createAndDestroyDeviceObject.cu b/examples/createAndDestroyDeviceObject.cpp similarity index 66% rename from examples/createAndDestroyDeviceObject.cu rename to examples/createAndDestroyDeviceObject.cpp index 1a6a2734d..cc67bc54b 100644 --- a/examples/createAndDestroyDeviceObject.cu +++ b/examples/createAndDestroyDeviceObject.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ -#include // createDeviceArray, destroyDeviceArray +#include // createDeviceArray, destroyDeviceArray, createHostArray, destroyHostArray #include // STDGPU_HOST_DEVICE @@ -44,6 +44,27 @@ class Image device_object._height = 0; } + static Image + createHostObject(const stdgpu::index_t width, + const stdgpu::index_t height) + { + Image result; + + result._values = createHostArray(width * height); + result._width = width; + result._height = height; + + return result; + } + + static void + destroyHostObject(Image& host_object) + { + destroyHostArray(host_object._values); + host_object._width = 0; + host_object._height = 0; + } + // Further (static) member functions ... STDGPU_HOST_DEVICE std::uint8_t& @@ -72,32 +93,29 @@ class Image }; -__global__ void -fill_image(Image d_image) +void +fill_image(Image image) { - stdgpu::index_t i = blockIdx.x * blockDim.x + threadIdx.x; - stdgpu::index_t j = blockIdx.y * blockDim.y + threadIdx.y; - - if (i >= d_image.width() || j >= d_image.height()) return; - - std::uint8_t value = (i * i + j * j) % (1 << 8); + for (stdgpu::index_t i = 0; i < image.width(); ++i) + { + for (stdgpu::index_t j = 0; j < image.height(); ++j) + { + std::uint8_t value = (i * i + j * j) % (1 << 8); - d_image(i, j) = value; + image(i, j) = value; + } + } } int main() { - Image d_image = Image::createDeviceObject(1920, 1080); - - dim3 threads(32, 8); - dim3 blocks((d_image.width() + threads.x - 1) / threads.x, (d_image.height() + threads.y - 1) / threads.y); + Image image = Image::createHostObject(1920, 1080); - fill_image<<< blocks, threads >>>(d_image); - cudaDeviceSynchronize(); + fill_image(image); - Image::destroyDeviceObject(d_image); + Image::destroyHostObject(image); } diff --git a/examples/cuda/CMakeLists.txt b/examples/cuda/CMakeLists.txt new file mode 100644 index 000000000..b562dfa20 --- /dev/null +++ b/examples/cuda/CMakeLists.txt @@ -0,0 +1,11 @@ + +macro(stdgpu_add_example_cu) + stdgpu_detail_add_example(${ARGV0} "cu") +endmacro() + +stdgpu_add_example_cu(bitset) +stdgpu_add_example_cu(container_iterator) +stdgpu_add_example_cu(container_kernel) +stdgpu_add_example_cu(mutex_array) +stdgpu_add_example_cu(thrust_interoperability) +stdgpu_add_example_cu(thrust_towards_ranges) diff --git a/examples/bitset.cu b/examples/cuda/bitset.cu similarity index 100% rename from examples/bitset.cu rename to examples/cuda/bitset.cu diff --git a/examples/container_iterator.cu b/examples/cuda/container_iterator.cu similarity index 100% rename from examples/container_iterator.cu rename to examples/cuda/container_iterator.cu diff --git a/examples/container_kernel.cu b/examples/cuda/container_kernel.cu similarity index 100% rename from examples/container_kernel.cu rename to examples/cuda/container_kernel.cu diff --git a/examples/mutex_array.cu b/examples/cuda/mutex_array.cu similarity index 100% rename from examples/mutex_array.cu rename to examples/cuda/mutex_array.cu diff --git a/examples/thrust_interoperability.cu b/examples/cuda/thrust_interoperability.cu similarity index 98% rename from examples/thrust_interoperability.cu rename to examples/cuda/thrust_interoperability.cu index 314402093..0e7f62c59 100644 --- a/examples/thrust_interoperability.cu +++ b/examples/cuda/thrust_interoperability.cu @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include #include #include diff --git a/examples/thrust_towards_ranges.cu b/examples/cuda/thrust_towards_ranges.cu similarity index 99% rename from examples/thrust_towards_ranges.cu rename to examples/cuda/thrust_towards_ranges.cu index 1ad36efa9..2280cba18 100644 --- a/examples/thrust_towards_ranges.cu +++ b/examples/cuda/thrust_towards_ranges.cu @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include #include #include