---
title: "CUDAMemoryLoad"
language: "en"
type: "Symbol"
summary: "CUDAMemoryLoad[list] registers list into the CUDALink memory manager. CUDAMemoryLoad[img] registers img into the CUDALink memory manager."
keywords: 
- cuda memory load
- gpu memory load
- CUDAMemoryPut
- CUDAMemoryAdd
- add memory
canonical_url: "https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryLoad.html"
source: "Wolfram Language Documentation"
related_guides: 
  - 
    title: "CUDALink"
    link: "https://reference.wolfram.com/language/CUDALink/guide/CUDALink.en.md"
related_functions: 
  - 
    title: "CUDAMemoryLoad"
    link: "https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryLoad.en.md"
  - 
    title: "CUDAMemoryAllocate"
    link: "https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryAllocate.en.md"
  - 
    title: "CUDAMemoryGet"
    link: "https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryGet.en.md"
  - 
    title: "CUDAMemoryUnload"
    link: "https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryUnload.en.md"
  - 
    title: "CUDAMemoryInformation"
    link: "https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryInformation.en.md"
related_tutorials: 
  - 
    title: "CUDALink User Guide"
    link: "https://reference.wolfram.com/language/CUDALink/tutorial/Overview.en.md"
  - 
    title: "CUDALink Memory"
    link: "https://reference.wolfram.com/language/CUDALink/tutorial/Memory.en.md"
  - 
    title: "CUDALink Programming"
    link: "https://reference.wolfram.com/language/CUDALink/tutorial/Programming.en.md"
---
## CUDALink\`

# CUDAMemoryLoad

CUDAMemoryLoad[list] registers list into the *CUDALink* memory manager.

CUDAMemoryLoad[img] registers img into the *CUDALink* memory manager.

## Details and Options

* The *CUDALink* application must be loaded using ``Needs["CUDALink`"]``.

* Possible types for ``CUDAMemoryLoad`` are:

|                   |                    |                      |
| ----------------- | ------------------ | -------------------- |
| Integer           | Real               | Complex              |
| "Byte"            | "Bit16"            | "Integer"            |
| "Byte[2]"         | "Bit16[2]"         | "Integer32[2]"       |
| "Byte[3]"         | "Bit16[3]"         | "Integer32[3]"       |
| "Byte[4]"         | "Bit16[4]"         | "Integer32[4]"       |
| "UnsignedByte"    | "UnsignedBit16"    | "UnsignedInteger"    |
| "UnsignedByte[2]" | "UnsignedBit16[2]" | "UnsignedInteger[2]" |
| "UnsignedByte[3]" | "UnsignedBit16[3]" | "UnsignedInteger[3]" |
| "UnsignedByte[4]" | "UnsignedBit16[4]" | "UnsignedInteger[4]" |
| "Double"          | "Float"            | "Integer64"          |
| "Double[2]"       | "Float[2]"         | "Integer64[2]"       |
| "Double[3]"       | "Float[3]"         | "Integer64[3]"       |
| "Double[4]"       | "Float[4]"         | "Integer64[4]"       |

* The following options can be given:

|                    |              |                                 |
| ------------------ | ------------ | ------------------------------- |
| "Device"           | \$CUDADevice | CUDA device used in computation |
| "TargetPrecision"  | Automatic    | precision used in computation   |

## Examples (8)

### Basic Examples (1)

First, load the *CUDALink* application:

```wl
In[1]:= Needs["CUDALink`"]
```

This loads memory:

```wl
In[2]:= mem = CUDAMemoryLoad[{1, 2, 3}]

Out[2]= CUDAMemory["<27339>", "Integer64"]
```

Information about memory can be retrieved using ``CUDAMemoryInformation`` :

```wl
In[3]:= CUDAMemoryInformation[mem]

Out[3]= {"ID" -> 27339, "HostStatus" -> "Synchronized", "DeviceStatus" -> "Uninitialized", "Residence" -> "DeviceHost", "Sharing" -> "Cloned", "Type" -> "Integer64", "ByteCount" -> 24, "Dimensions" -> {3}, "Device" -> 1, "MathematicaType" -> List, "TypeInformation" -> {}}
```

Memory added must be freed with ``CUDAMemoryUnload`` :

```wl
In[4]:= CUDAMemoryUnload[mem]
```

### Scope (4)

Adding memory as ``Real`` or ``Complex`` gets the type based on whether the device supports double precision or not:

```wl
In[1]:= CUDAMemoryLoad[{1.0, 2.0, 3.0}, Real]

Out[1]= CUDAMemory["<16286>", "Double"]
```

In this case, the CUDA device has double-precision support:

```wl
In[2]:= CUDAInformation[$CUDADevice, "Compute Capabilities"]

Out[2]= 7.5
```

The behavior can be forced to change by setting the ``"TargetPrecision"`` :

```wl
In[3]:= CUDAMemoryLoad[{1.0, 2.0, 3.0}, Real, "TargetPrecision" -> "Single"]

Out[3]= CUDAMemory["<10464>", "Float"]
```

Memory added must be freed with ``CUDAMemoryUnload`` :

```wl
In[4]:= CUDAMemoryUnload[%]
```

---

Images can be added with type ``"UnsignedByte"`` :

```wl
In[1]:= mem = CUDAMemoryLoad[[image]]

Out[1]= CUDAMemory["<15705>", "UnsignedByte"]
```

Getting the memory returns an image with the same properties as the original. Memory is retrieved using ``CUDAMemoryGet`` :

```wl
In[2]:= CUDAMemoryGet[mem]

Out[2]= [image]
```

The ``"TypeInformation"`` contains the image information:

```wl
In[3]:= CUDAMemoryInformation[mem]

Out[3]= {"ID" -> 15705, "HostStatus" -> "Synchronized", "DeviceStatus" -> "Uninitialized", "Residence" -> "DeviceHost", "Sharing" -> "Cloned", "Type" -> "UnsignedByte", "ByteCount" -> 67500, "Dimensions" -> {150, 150, 3}, "Device" -> 1, "MathematicaType" - ...  -> "RGB", "DataMaxValue" -> 255, "DataRange" -> {0, 255}, "DataType" -> "Byte", "Dimensions" -> {150, 150, 3}, "Height" -> 150, "ImageDimensions" -> {150, 150}, "Interleaving" -> True, "SampleDepth" -> 8, "Transparency" -> False, "Width" -> 150}}
```

Memory added must be freed with ``CUDAMemoryUnload`` :

```wl
In[4]:= CUDAMemoryUnload[mem]
```

---

Images can be added with specified type:

```wl
In[1]:= CUDAMemoryLoad[[image], "Float"]

Out[1]= CUDAMemory["<14561>", "Float"]
```

Getting the memory returns an image:

```wl
In[2]:= CUDAMemoryGet[%]

Out[2]= [image]
```

---

When adding graphics objects, the object is rasterized:

```wl
In[1]:= mem = CUDAMemoryLoad[[image]]

Out[1]= CUDAMemory["<24157>", "UnsignedByte"]
```

When getting the memory, an image is returned:

```wl
In[2]:= CUDAMemoryGet[mem]//Head

Out[2]= Image
```

Memory added must be freed with ``CUDAMemoryUnload`` :

```wl
In[3]:= CUDAMemoryUnload[mem]
```

### Options (1)

#### "TargetPrecision" (1)

Adding memory as ``Real`` or ``Complex`` gets the type based on whether the device supports double precision or not:

```wl
In[1]:= CUDAMemoryLoad[{1.0, 2.0, 3.0}, Real]

Out[1]= CUDAMemory["<7389>", "Double"]
```

This can be overridden by setting the ``"TargetPrecision"`` option to either ``"Single"`` or ``"Double"``. In this case, the machine has double-precision hardware, but a ``"Float"`` is used because the ``"TargetPrecision"`` is single:

```wl
In[2]:= CUDAMemoryLoad[{1.0, 2.0, 3.0}, Real, "TargetPrecision" -> "Single"]

Out[2]= CUDAMemory["<12018>", "Float"]
```

If type is always single precision, then setting the type as ``"Float"`` or ``"ComplexFloat"`` may be more readable:

```wl
In[3]:= CUDAMemoryLoad[{1.0, 2.0, 3.0}, "Float"]

Out[3]= CUDAMemory["<16549>", "Float"]
```

### Applications (2)

This adds two to an input list:

```wl
In[1]:= srcf = FileNameJoin[{$CUDALinkPath, "SupportFiles", "addTwo.cu"}]

Out[1]= "C:\\Users\\arnou\\AppData\\Roaming\\Mathematica\\Paclets\\Repository\\CUDALink-12.2.1\\SupportFiles\\addTwo.cu"

In[2]:= addTwo = CUDAFunctionLoad[{srcf}, "addTwo", {{_Integer}, _Integer}, 32]

Out[2]= CUDAFunction["<>", "addTwo", {{_Integer}, "Integer64"}]

In[3]:= res = addTwo[CUDAMemoryLoad[Range[100]], 100]

Out[3]= {CUDAMemory["<24998>", "Integer64"]}

In[4]:= CUDAMemoryGet[First@res]

Out[4]= {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102}

In[5]:= CUDAMemoryUnload[First@res]
```

---

Internally, running a ``CUDAFunction`` will load and (if the input is not ``CUDAMemory``) will unload the memory. To demonstrate, this color negates an input image:

```wl
In[1]:=
src = "__global__ void imageColorNegate(mint * img, mint width, mint height, mint channels) {
    mint ii;
	mint xIndex = threadIdx.x + blockIdx.x*blockDim.x;
	mint yIndex = threadIdx.y + blockIdx.y*blockDim.y;
	mint index = channels*(xIndex + yIndex*width);
	if (xIndex < width && yIndex < height) {
		for (ii = 0; ii < channels; ii++)
			img[index+ii] = 255 - img[index+ii];
    }
}";
```

This loads the function using ``CUDAFunctionLoad`` :

```wl
In[2]:= colorNegate = CUDAFunctionLoad[src, "imageColorNegate", {{_Integer}, _Integer, _Integer, _Integer}, {16, 16}]

Out[2]= CUDAFunction["<>", "imageColorNegate", {{_Integer}, "Integer64", "Integer64", "Integer64"}]
```

This defines input parameters:

```wl
In[3]:=
{height, width} = ImageDimensions[[image]]
channels = ImageChannels[[image]]

Out[3]= {150, 150}

Out[3]= 3
```

This runs the ``CUDAFunction`` :

```wl
In[4]:= colorNegate[[image], width, height, channels]

Out[4]= {[image]}
```

## See Also

* [CUDAMemoryLoad](https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryLoad.en.md)
* [CUDAMemoryAllocate](https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryAllocate.en.md)
* [CUDAMemoryGet](https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryGet.en.md)
* [CUDAMemoryUnload](https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryUnload.en.md)
* [CUDAMemoryInformation](https://reference.wolfram.com/language/CUDALink/ref/CUDAMemoryInformation.en.md)

## Tech Notes

* [*CUDALink* User Guide](https://reference.wolfram.com/language/CUDALink/tutorial/Overview.en.md)
* [*CUDALink* Memory](https://reference.wolfram.com/language/CUDALink/tutorial/Memory.en.md)
* [*CUDALink* Programming](https://reference.wolfram.com/language/CUDALink/tutorial/Programming.en.md)

## Related Guides

* [CUDALink](https://reference.wolfram.com/language/CUDALink/guide/CUDALink.en.md)