Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: Split CUDA-specific files #27

Merged
merged 2 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

#include <stdgpu/memory.h> // createDeviceArray, destroyDeviceArray
#include <stdgpu/memory.h> // createDeviceArray, destroyDeviceArray, createHostArray, destroyHostArray
#include <stdgpu/platform.h> // STDGPU_HOST_DEVICE


Expand Down Expand Up @@ -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<std::uint8_t>(width * height);
result._width = width;
result._height = height;

return result;
}

static void
destroyHostObject(Image& host_object)
{
destroyHostArray<std::uint8_t>(host_object._values);
host_object._width = 0;
host_object._height = 0;
}

// Further (static) member functions ...

STDGPU_HOST_DEVICE std::uint8_t&
Expand Down Expand Up @@ -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);
}


11 changes: 11 additions & 0 deletions examples/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

#include <iostream>
#include <thrust/reduce.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

#include <iostream>
#include <thrust/for_each.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
Expand Down