Skip to content

Commit ef7751d

Browse files
author
Pietro Vertechi
authored
document custom array usage (#46)
1 parent 9c2f87e commit ef7751d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,46 @@ julia> rand!(s)
7272
0.0340059+0.420472im 0.907252+0.808263im
7373
```
7474

75+
### Using custom array types
76+
77+
StructArrays supports using custom array types. It is always possible to pass field arrays of a custom type. The "custom array of structs to struct of custom arrays" transformation will use the `similar` method of the custom array type. This can be useful when working on the GPU for example:
78+
79+
```julia
80+
julia> using StructArrays, CuArrays
81+
82+
julia> a = CuArray(rand(Float32, 10));
83+
84+
julia> b = CuArray(rand(Float32, 10));
85+
86+
julia> StructArray{ComplexF32}((a, b))
87+
10-element StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1},CuArray{Float32,1}}}}:
88+
0.7539003f0 + 0.5406891f0im
89+
0.2818451f0 + 0.60345674f0im
90+
0.3271774f0 + 0.56674314f0im
91+
0.6943406f0 + 0.8360009f0im
92+
0.9609026f0 + 0.27519035f0im
93+
0.051933408f0 + 0.93443274f0im
94+
0.51335454f0 + 0.90320504f0im
95+
0.6588727f0 + 0.16270757f0im
96+
0.20075476f0 + 0.6591008f0im
97+
0.58832633f0 + 0.45309567f0im
98+
99+
julia> c = CuArray(rand(ComplexF32, 10));
100+
101+
julia> StructArray(c)
102+
10-element StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1},CuArray{Float32,1}}}}:
103+
0.76695776f0 + 0.31588173f0im
104+
0.9804857f0 + 0.15740407f0im
105+
0.85849273f0 + 0.51903546f0im
106+
0.106796384f0 + 0.9493377f0im
107+
0.38152885f0 + 0.8419838f0im
108+
0.8892112f0 + 0.5276251f0im
109+
0.11579132f0 + 0.79168653f0im
110+
0.16804445f0 + 0.40545344f0im
111+
0.42822742f0 + 0.61150527f0im
112+
0.29996157f0 + 0.94151044f0im
113+
```
114+
75115
## Example usage to store a data table
76116

77117
```julia
@@ -94,3 +134,4 @@ julia> push!(t, (a = 3, b = "z"))
94134
(a = 2, b = "y")
95135
(a = 3, b = "z")
96136
```
137+

0 commit comments

Comments
 (0)