8000 A few examples for ArrayFire fortran wrapper · arrayfire/arrayfire-fortran@c29265a · GitHub
[go: up one dir, main page]

Skip to content

Commit c29265a

Browse files
committed
A few examples for ArrayFire fortran wrapper
1 parent ecc983e commit c29265a

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*_cuda
22
*_ocl
3+
*.mod

examples/Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
AF_FORT_PATH?=$(shell (dirname `pwd`))
2+
3+
-include $(AF_FORT_PATH)/common.mk
4+
5+
AF_FORT_MOD = $(AF_FORT_PATH)/src/arrayfire.f95
6+
LDFLAGS += -Wl,--no-as-needed -L$(AF_FORT_LIB_PATH) -l$(AF_FORT) -L$(AF_LIB_PATH) -l$(AF)
7+
LDFLAGS += -Wl,-rpath,$(AF_FORT_LIB_PATH),-rpath,$(abspath $(AF_FORT_LIB_PATH))
8+
LDFLAGS += -Wl,-rpath,$(AF_LIB_PATH),-rpath,$(abspath $(AF_LIB_PATH))
9+
10+
SRC:=$(wildcard *.f95)
11+
BIN:=$(patsubst %.f95, %_$(EXT), $(SRC))
12+
13+
cuda: $(BIN)
14+
15+
opencl: $(BIN)
16+
17+
%_ocl: %.f95 $(AF_FORT_MOD)
18+
gfortran -L$(AF_PATH)/$(LIB) $(CFLAGS) $(LDFLAGS) $(AF_FORT_MOD) -o $@ $<
19+
20+
%_cuda: %.f95 $(AF_FORT_MOD)
21+
gfortran -L$(AF_PATH)/$(LIB) $(CFLAGS) $(LDFLAGS) $(AF_FORT_MOD) -o $@ $<
22+
23+
clean:
24+
rm -f *_ocl *_cuda *.mod

examples/basic.f95

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
program basic
2+
use arrayfire
3+
implicit none
4+
5+
real, dimension(3,3) :: a
6+
real, dimension(:,:), allocatable :: b
7+
type(array) M1, M2, tmp
8+
9+
a(1,:) = (/ 1, 0, 0 /)
10+
a(2,:) = (/ 0, 3, 0 /)
11+
a(3,:) = (/ 0, 0, 2 /)
12+
13+
! Copy data from host to device
14+
M1 = a
15+
write(*,*) "Showing the matrix after mem copy from host (M1)"
16+
call print(M1)
17+
18+
! Generate a uniformly random, single precision matrix
19+
M2 = randu(3,3, ty=f32)
20+
write(*,*) "Showing a randomly generated matrix (M2)"
21+
call print(M2)
22+
23+
! Transpose of matrix
24+
tmp = transpose(M2)
25+
call print(tmp, "Transpose of M2") ! Displays array after printing message
26+
27+
! Element wise addition
28+
tmp = M1 + M2
29+
call print(tmp, "M1 + M2")
30+
31+
! element wise subtraction
32+
call print(-M2, "-M2")
33+
34+
! Trignometric functions
35+
write(*,*) "Displaying sin(M2)**2 + cos(M2)**2"
36+
call print(sin(M2)**2.0 + cos(M2)**2.0)
37+
38+
! Multiplication of matrices
39+
! Matrix multiply
40+
write(*, *) "Matrix multiply: matmul(M1, M2)"
41+
call print(matmul(M1, M2))
42+
43+
! Element wise multiplication
44+
write(*, *) "Element wise multiplication: M1 * M2"
45+
call print(M1 * M2)
46+
47+
! minimum value
48+
tmp = min(M2)
49+
call print(tmp, "min(M2)")
50+
51+
! Get back to host
52+
b = tmp
53+
write(*,*) "Showing min(M2) data back on host "
54+
write(*,*) b(:,:)
55+
56+
end program basic

examples/dla.f95

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
program dla
2+
use arrayfire
3+
implicit none
4+
5+
type(array) A, B, C, X0, X1, X2
6+
type(array) l, u, p, r, diff
7+
integer :: N
8+
9+
! Set the size of the matrix
10+
N = 5
11+
12+
! Randomly generate a system
13+
A = randu(N, N)
14+
X0 = randu(N, 1)
15+
16+
! LU decomposition
17+
call lu(l, u, p, A)
18+
19+
! Construct a positive definite matrix
20+
C = A + transpose(A) + identity(N, N) * 100.0
21+
call cholesky(r, C)
22+
23+
! Solve a general system of equations
24+
B = matmul(A, X0)
25+
X1 = solve(A, B)
26+
call print(max(abs(X0 - X1)), "absolute error: Solving general system")
27+
28+
! Solve a positive definite system of equations
29+
B = matmul(C, X0)
30+
X2 = solve(C, B)
31+
call print(max(abs(X0 - X2)), "absolute error: Solving positive definite system")
32+
33+
! Invert a matrix
34+
r = inverse(A)
35+
diff = abs(identity(N, N) - matmul(r, A))
36+
call print(max(moddims(diff, N * N, 1)), "absolute error: Inverting a matrix")
37+
38+
! Matrix power
39+
r = matpow(A, 3)
40+
diff = abs(r - matmul(matmul(A, A), A))
41+
call print(max(moddims(diff, N * N, 1)), "absolute error: Matrix cube")
42+
43+
end program dla

examples/indexing.f95

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
program template
2+
use arrayfire
3+
implicit none
4+
5+
type(array) A1, A2, tmp
6+
integer :: I1(2), I2(2)
7+
8+
! 1D indexing
9+
A1 = randu(5, 1)
10+
A2 = constant(0, 5, 1)
11+
tmp = get(A1, seq(3,5)) ! Get elements 3 through 5
12+
call set(A2, tmp, seq(1,3)) ! Set elements 1 through 3 with values from tmp
13+
call print(A1,"A1")
14+
call print(tmp, "tmp")
15+
call print(A2,"A2")
16+
17+
! 2D indexing
18+
A1 = randu(3,3)
19+
A2 = constant(1,3,3)
20+
I1 = (/ 1, 3 /)
21+
I2 = (/ 2, 3 /)
22+
tmp = get(A1, idx(I1), idx(I2)) ! Get rows 1 and 3 for columns 2 and 3
23+
call set(A2, tmp, idx(I2), idx(I1)) ! Set rows 2 and 3 for columns 1 and 3 with values from tmp
24+
call print(A1, "A1")
25+
call print(tmp, "tmp")
26+
call print(A2, "A2")
27+
28+
! 3D indexing
29+
A1 = randu(3,3,2)
30+
A2 = constant(1,3,3,2)
31+
I1 = (/ 1, 3 /)
32+
I2 = (/ 2, 3 /)
33+
tmp = get(A1, idx(I1), seq(1,3,2), 1) ! Get rows 1 and 3 for columns 1 and 3, tile 1
34+
call set(A2, tmp, idx(I2), seq(1,2), 2) ! Set rows 2 and 3 for columns 1 and 2, tile 2 with tmp
35+
call print(A1, "A1")
36+
call print(tmp, "tmp")
37+
call print(A2, "A2")
38+
39+
end program template

examples/template.f95

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
program template
2+
use arrayfire
3+
implicit none
4+
5+
call device_info()
6+
7+
end program template

examples/timer.f95

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
program timer
2+
use arrayfire
3+
implicit none
4+
double precision elapsed
5+
type(array) A, B
6+
7+
! Generate a random matrix
8+
A = randu(1024, 2048)
9+
10+
! Start the timer
11+
call timer_start()
12+
13+
! Perform operations here.
14+
B = matmul(A, transpose(A))
15+
16+
! Stop the timer
17+
elapsed = timer_stop()
18+
19+
! Print time taken
20+
write (*,"(a15, d8.2)") "Time taken: ", elapsed
21+
end program timer

0 commit comments

Comments
 (0)
0