Language Julia: convert vector in matrix - matrix

Who can help.
To transform a vector into a one-dimensional matrix just run in Julia:
a = copy(permutedims([1,2,3]))
To transform the matrix "a" into a vector just use:
b = copy(vec(a))
If you have a matrix "[1 2 3; 4 5 6]" to transform it into a vector, just:
c = vec([1 2 3; 4 5 6])
Now how to make the vector have the form of the 2x3 matrix like:
2×3 Matrix{Int64}:
1 2 3
4 5 6

You can use reshape
julia> c = vec([1 2 3; 4 5 6])
6-element Vector{Int64}:
1
4
2
5
3
6
julia> M=reshape(c,2,3)
2×3 Matrix{Int64}:
1 2 3
4 5 6
Note that this operation does not reallocate memory, c and M share the same memory. By example:
julia> c[1]=10
10
julia> M
2×3 Matrix{Int64}:
10 2 3
4 5 6

Related

Intersection of two matrices in Julia?

Is there any function or method in Julia that would allow one to compute the intersection of two matrices A and B?
There are many possible definitions for intersection.
Suppose that you have:
julia> a=[1 2 3;4 5 6;3 2 1]
3×3 Matrix{Int64}:
1 2 3
4 5 6
3 2 1
julia> b = a'
3×3 adjoint(::Matrix{Int64}) with eltype Int64:
1 4 3
2 5 2
3 6 1
and if you mean by intersection you mean matrix with elements who have the same values in both matrices and zero otherwise you could do:
julia> (a .== b) .* a
3×3 Matrix{Int64}:
1 0 3
0 5 0
3 0 1

Julia matrix row becomes a column during indexing

Issue: When building a matrix out of single rows, Julia interprets them as columns instead.
a = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
b = [a[1:2,:]; a[1:2,:]] # Rows duplicated correctly
4×3 Array{Int64,2}:
1 2 3
4 5 6
1 2 3
4 5 6
c = [a[1,:]; a[1,:]] # Rows converted to columns
6-element Array{Int64,1}:
1
2
3
1
2
3
How to fix this?
In addition to the range index, you can transpose vectors
julia> [a[1, :]'; a[1, :]']
2×3 Array{Int64,2}:
1 2 3
1 2 3
It looks likes this approach is somewhat more performant, than the range index, but it should be tested on larger matrices, also it is not consistent, if you have ranges and single columns
using BenchmarkTools
f1(a) = [a[1:1,:]; a[1:1,:]]
f2(a) = [a[1, :]'; a[1, :]']
julia> #btime f1($a)
122.440 ns (3 allocations: 352 bytes)
2×3 Array{Int64,2}:
1 2 3
1 2 3
julia> #btime f2($a)
107.480 ns (3 allocations: 352 bytes)
2×3 Array{Int64,2}:
1 2 3
1 2 3
Solution: Although it may feel a bit perplexing, its because the type has changed from a matrix into a vector. To keep the type the same you'll need to select from your desired row, to your desired row in a similar manner to the second line of code in your example.
c = [a[1:1,:]; a[1:1,:]]
2×3 Array{Int64,2}:
1 2 3
1 2 3

Julialang: In-place sort for columns of a matrix

I have a huge matrix and want to sort the columns in place for speed/memory efficency. Is it possible to use in-place sort for the columns of a matrix in Julia?
As an example, take the following matrix:
julia> M=Matrix{Int}(3,3);
julia> for i = 1:size(M)[1]
for j = 1:size(M)[2]
M[i,j]=3*(j-1)+i
end
end
julia> M
3×3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
I want to use in-place sort for the columns to obtain the matrix
3×3 Array{Int64,2}:
3 6 9
2 5 8
1 4 7
This can be obtained without in-place sort as follows:
julia> M_sorted=Matrix{Int}(3,3);
julia> for j = 1:size(M)[2]
M_sorted[:,j]=sort(M[:,j],rev=true)
end
julia> M_sorted
3×3 Array{Int64,2}:
3 6 9
2 5 8
1 4 7
But something like that fails (here only for one column):
julia> sort!(M[:,1],rev=true)
3-element Array{Int64,1}:
3
2
1
julia> M
3×3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
Is there any way to use in-place sort in this case? Note that there is no problem with the indexing since the matrix is saved column-wise in memory:
julia> M[1:end]
9-element Array{Int64,1}:
1
2
3
4
5
6
7
8
9
So I think it should be possible.
Slicing creates a copy of the column and sorts on that. If you instead want to directly sort in the memory of the existing array, use a view. Example:
M=Matrix{Int}(undef,3,3)
for i = 1:size(M)[1]
for j = 1:size(M)[2]
M[i,j]=3*(j-1)+i
end
end
M
3×3 Array{Int64,2}:
1 4 7
2 5 8
3 6 9
sort!(#view(M[:,1]),rev=true)
M
3×3 Array{Int64,2}:
3 4 7
2 5 8
1 6 9

CUDA - Extract Layer from 3D array

I have a 3D matrix where the x-y plane(s) represent an image and the z-plane represents image layers.
The issue is when I try to extract the first (or other layers) using idz, I do not get the expected results. It looks like the array, once in CUDA, has different indexes for x, y or z than what I expect (as in pycuda). I see this by the result array below.
The following is a step by step process for this mini example (I used generic int numbers to represent my images to save uploading images and the entire code)!
Here I import libraries and define image size and layers...
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
from pycuda.gpuarray import to_gpu
row = 10
column = 10
depth = 5
Then I define my input 3D array and my output 2D array...
#--==== Input 3D Array ====---
arrayA = numpy.full((row, column, depth), 0)
#populate each layer with fixed values
for i in range(depth):
arrayA[:,:,i] = i + 1
arrayA = arrayA.astype(numpy.uint16)
arrayA_gpu = cuda.mem_alloc(arrayA.nbytes)
cuda.memcpy_htod(arrayA_gpu, arrayA)
arrayA_Answer = numpy.empty_like(arrayA)
#--==== Output 2D array container ====---
arrayB = numpy.zeros([row, column], dtype = numpy.uint16)
arrayB_gpu = cuda.mem_alloc(arrayB.nbytes)
cuda.memcpy_htod(arrayB_gpu, arrayB)
arrayB_Answer = numpy.empty_like(arrayB)
Next I define the CUDA kernal and function in pycuda
mod = SourceModule("""
__global__ void getLayer(int *arrayA, int *arrayB)
{
int idx = threadIdx.x + (blockIdx.x * blockDim.x); // x coordinate (numpy axis 2)
int idy = threadIdx.y + (blockIdx.y * blockDim.y); // y coordinate (numpy axis 1)
int idz = 0; //The first layer, this can set in range from 0-4
int x_width = (blockDim.x * gridDim.x);
int y_width = (blockDim.y * gridDim.y);
arrayB[idx + (x_width * idy)] = arrayA[idx + (x_width * idy) + (x_width * y_width) * idz];
}
""")
func = mod.get_function("getLayer")
func(arrayA_gpu, arrayB_gpu, block=(row, column, 1), grid=(1,1))
Using standard pycuda commands, I extract the results (not what I expected)
arrayA[:,:,0] = 10x10 matrix populated with 1's (good)
print(arrayA_Answer[:,:,0])
[[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1 1 1]]
arrayB[:,:] = 10x10 matrix populated with the following (bad), expected to be equal to arrayA[:,:,0]...
print(arrayB_Answer)
[[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]
[1 2 3 4 5 1 2 3 4 5]]
As discussed here, the numpy 3D storage order pattern is that the "z" (i.e. "3rd") index is the rapidly varying index, as you progress linearly through memory. Your code assumes that the first index ("x") is the rapidly varying one.
Since your kernel is already organized for efficient ("coalesced") load/store behavior, you could address this by reordering the storage of your images/layers/slices in numpy. Here is a worked example:
$ cat t10.py
from __future__ import print_function
import pycuda.driver as cuda
import pycuda.autoinit
from pycuda.compiler import SourceModule
import numpy
from pycuda.gpuarray import to_gpu
row = 5
column = 10
depth = 10
#--==== Input 3D Array ====---
arrayA = numpy.full((row, column, depth), 0)
my_slice=numpy.int32(3) # choose the layer
#populate each layer with fixed values
for i in range(row):
arrayA[i,:,:] = i + 1
arrayA = arrayA.astype(numpy.int32)
arrayA_gpu = cuda.mem_alloc(arrayA.nbytes)
cuda.memcpy_htod(arrayA_gpu, arrayA)
arrayA_Answer = numpy.empty_like(arrayA)
#--==== Output 2D array container ====---
arrayB = numpy.zeros([column, depth], dtype = numpy.int32)
arrayB_gpu = cuda.mem_alloc(arrayB.nbytes)
cuda.memcpy_htod(arrayB_gpu, arrayB)
arrayB_Answer = numpy.empty_like(arrayB)
mod = SourceModule("""
__global__ void getLayer(int *arrayA, int *arrayB, int slice)
{
int idx = threadIdx.x + (blockIdx.x * blockDim.x); // x coordinate (numpy axis 2)
int idy = threadIdx.y + (blockIdx.y * blockDim.y); // y coordinate (numpy axis 1)
int idz = slice; //The "layer"
int x_width = (blockDim.x * gridDim.x);
int y_width = (blockDim.y * gridDim.y);
arrayB[idx + (x_width * idy)] = arrayA[idx + (x_width * idy) + (x_width * y_width) * idz];
}
""")
func = mod.get_function("getLayer")
func(arrayA_gpu, arrayB_gpu, my_slice, block=(depth, column, 1), grid=(1,1))
cuda.memcpy_dtoh(arrayB_Answer,arrayB_gpu)
print(arrayA[my_slice,:,:])
print(arrayB_Answer[:,:])
$ python t10.py
[[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]]
[[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]
[4 4 4 4 4 4 4 4 4 4]]
$
Note that I have also changed your use of uint16 to int32, to match the kernel type int.

Tiling or repeating n-dimensional arrays in Julia

I am looking for a general function to tile or repeat matrices along an arbitrary number of dimensions an arbitrary number of times. Python and Matlab have these features in NumPy's tile and Matlab's repmat functions. Julia's repmat function only seems to support up to 2-dimensional arrays.
The function should look like repmatnd(a, (n1,n2,...,nk)). a is an array of arbitrary dimension. And the second argument is a tuple specifying the number of times the array is repeated for each dimension k.
Any idea how to tile a Julia array on greater than 2 dimensions? In Python I would use np.tile and in matlab repmat, but the repmat function in Julia only supports 2 dimensions.
For instance,
x = [1 2 3]
repmatnd(x, 3, 1, 3)
Would result in:
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
1 2 3
And for
x = [1 2 3; 1 2 3; 1 2 3]
repmatnd(x, (1, 1, 3))
would result in the same thing as before. I imagine the Julia developers will implement something like this in the standard library, but until then, it would be nice to have a fix.
Use repeat:
julia> X = [1 2 3]
1x3 Array{Int64,2}:
1 2 3
julia> repeat(X, outer = [3, 1, 3])
3x3x3 Array{Int64,3}:
[:, :, 1] =
1 2 3
1 2 3
1 2 3
[:, :, 2] =
1 2 3
1 2 3
1 2 3
[:, :, 3] =
1 2 3
1 2 3
1 2 3

Resources