# can you use \`restrict\` on data members with cudaMalloc?

**URL:** https://forums.developer.nvidia.com/t/can-you-use-restrict-on-data-members-with-cudamalloc/57365
**Category:** CUDA Programming and Performance
**Created:** [January 23, 2018, 11:24am UTC](https://forums.developer.nvidia.com/t/can-you-use-restrict-on-data-members-with-cudamalloc/57365 "2018-01-23T11:24:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![svennevs](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/svennevs/32/40594_2.png) [@svennevs](https://forums.developer.nvidia.com/u/svennevs)
#### Post date: [January 23, 2018, 11:24am UTC](https://forums.developer.nvidia.com/t/can-you-use-restrict-on-data-members-with-cudamalloc/57365/1 "2018-01-23T11:24:00Z")

</div>

I am redoing some code to make sure I am taking advantage of **restrict** where applicable. For functions, it’s golden. But I’d like to also apply it to struct members, which is supposed to be valid: [http://en.cppreference.com/w/c/language/restrict#Struct\_members](http://en.cppreference.com/w/c/language/restrict#Struct_members)

However, I can’t figure out how to do this (or if it is even possible) because **cudaMalloc** needs a **void \*\***. Example

```auto
struct MyData {
    float4 * __restrict__ a = nullptr;
    float4 * __restrict__ b = nullptr;

    static const int N = 20;

    void allocate() {
        cudaMalloc(
            // reinterpret_cast<void **>(&a), // cannot cast away 'const' or other qualifiers
            &a,
            N * sizeof(float4)
        );
        cudaMalloc(
            &b,
            N * sizeof(float4)
        );            
    }

    void free() {
        cudaFree(a);
        cudaFree(b);
    }
};

int main(void) {
    MyData data;
    data.allocate();
    data.free();
}

```

Errors with

```auto
error: no instance of overloaded function "cudaMalloc" matches the argument list
            argument types are: (float4 * __restrict__ *, unsigned long)

```

In reality I think I have all of my bases covered, the methods where **a** and **b** are actually used in functions are marked as **restrict**. I was just wondering if the above is supported, planned to be, not possible to support, etc. Thanks for any thoughts!

---

<div class="post-metadata">

### Author: ![cbuchner1](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/cbuchner1/32/14168_2.png) [@cbuchner1](https://forums.developer.nvidia.com/u/cbuchner1)
#### Post date: [January 23, 2018, 3:42pm UTC](https://forums.developer.nvidia.com/t/can-you-use-restrict-on-data-members-with-cudamalloc/57365/2 "2018-01-23T15:42:41Z")

</div>

this variant builds for me with nvcc -std=c++11 -o test test.cu from the either the CUDA 7.5, 8.0 or the 9.1 toolkit.

sometimes the simplest typecasts are the best ;)

```auto
void allocate() {
            cudaMalloc(
                (void**)&a,
                N * sizeof(float4)
            );
            cudaMalloc(
                (void**)&b,
                N * sizeof(float4)
            );            
        }

```

---

<div class="post-metadata">

### Author: ![svennevs](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/svennevs/32/40594_2.png) [@svennevs](https://forums.developer.nvidia.com/u/svennevs)
#### Post date: [January 23, 2018, 11:48pm UTC](https://forums.developer.nvidia.com/t/can-you-use-restrict-on-data-members-with-cudamalloc/57365/3 "2018-01-23T23:48:21Z")

</div>

> [@](#):
>
> sometimes the simplest typecasts are the best ;)

Indeed! I can’t believe I didn’t even try that _faceslap_. Thanks :)
