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

Idea: Make DeviceMemory templated over a type T #261

Open
stijnh opened this issue Apr 12, 2024 · 3 comments
Open

Idea: Make DeviceMemory templated over a type T #261

stijnh opened this issue Apr 12, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@stijnh
Copy link

stijnh commented Apr 12, 2024

Is your feature request related to a problem? Please describe.
Currently, DeviceMemory (and also HostMemory and Array) just works on bytes. This can be error prone, for example, when performing a memcpy between two DeviceMemorys thats store data in a different type or when accidentally allocating n bytes (instead of n items, so n * sizeof(T) bytes)

Describe the solution you'd like
It could be useful to make these classes typesafe and let them store n items of type T. This would help to make the memcpy functions more type safe.

Describe alternatives you've considered
An alternative would be to add a new TypedDeviceMemory<T> type which internally stores a DeviceMemory to represents the data.

Additional context
N/A

@stijnh stijnh added the enhancement New feature or request label Apr 12, 2024
@stijnh stijnh changed the title Make DeviceMemory templated over a type T Idea: Make DeviceMemory templated over a type T Apr 12, 2024
@loostrum
Copy link
Member

loostrum commented Jul 9, 2024

@csbnw what do you think about this? I would find it quite useful to have type information embedded in DeviceMemory objects (and the same for HostMemory)

@csbnw
Copy link
Contributor

csbnw commented Jul 10, 2024

For me, having the size in bytes worked just fine for any of the existing codes that uses cudawrappers. I can see the advantages, though.

Having both XMemory and TypedXMemory<T> doesn't seem ideal. How about adding the template argument to the existing classes and have char as default value?

I would rather not break compatibility with existing code, so ideally the constructor should remain unchanged. Maybe we just rename size to n, to denote the number of elements of type T?

@stijnh
Copy link
Author

stijnh commented Jul 10, 2024

Using a default template argument like class DeviceMemory<typename T=char> could work. However users would need to explicitly use DeviceMemory<> (with an empty template argument list) to use the default type for T, so this will still break backwards compatibility.

Another alternative that I considered is to add a DeviceArray<T, N> class which stores an N-dimensional array of elements of type T. So the constructor would like N dimensions (one for each axis) and then automatically calculates the require number of bytes to store the array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants