SpM Handbook 1.2.4
Loading...
Searching...
No Matches
spm_user.jl
1#!/usr/bin/env julia
2#=
3 @file spm_user.jl
4
5 @brief Julia SpM example using a laplacian matrix.
6
7 @copyright 2019-2024 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
8 Univ. Bordeaux. All rights reserved.
9
10 @version 1.2.4
11 @author Mathieu Faverge
12 @author Selmane Lebdaoui
13 @author Tony Delarue
14 @date 2024-05-29
15
16 @ingroup examples_julia
17 @code
18
19=#
20using Pkg
21Pkg.activate("spm")
22Pkg.instantiate()
23using CBinding
24using spm
25
26using Base
27using Distributed
28
29if spm.spm_mpi_enabled
30 using MPI
31 MPI.Init()
32end
33
34A = spm.spmatrix_t(zero)
35Aptr = pointer_from_objref(A)
36
37#
38# Two solutions to select the outpu file to pass to output functions
39#
40my_stdout = Libc.FILE( Libc.RawFD(1), "w" ) # Select the stdout or stderr through 1 or 2
41#my_stdout = Ptr{Cvoid}(0) # Give a null pointer to use the default
42
43#
44#Generate a 10x10x10 complex Laplacian in IJV format
45#
46dim1 = 10
47dim2 = 10
48dim3 = 10
49n = dim1 * dim2 * dim3
50nnz = (2*(dim1)-1) * dim2 * dim3 + (dim2-1)*dim1*dim3 + dim2*dim1*(dim3-1)
51
52#Create the spm out of the internal data
53spm.spmInit( Aptr )
54A.baseval = 1
55A.mtxtype = spm.SpmSymmetric
56A.flttype = spm.SpmDouble
57A.fmttype = spm.SpmIJV
58A.n = n
59A.nnz = nnz
60A.dof = 1
61
62spm.spmUpdateComputedFields( Aptr )
63
64# Allocate the arrays of the spm through C functions
65spm.spmAlloc( Aptr )
66
67# Get the pointer to the allocated arrays
68row = unsafe_wrap(Array, A.rowptr, A.nnzexp, own = false)
69col = unsafe_wrap(Array, A.colptr, A.nnzexp, own = false)
70val = unsafe_wrap(Array{Cdouble,1}, convert( Ptr{Cdouble}, A.values ), A.nnzexp, own = false)
71
72m = 1
73for i in 1:dim1
74 for j in 1:dim2
75 for k in 1:dim3
76 global m
77 row[m] = (i-1) + dim1 * (j-1) + dim1 * dim2 * (k-1) + 1
78 col[m] = (i-1) + dim1 * (j-1) + dim1 * dim2 * (k-1) + 1
79 val[m] = 6.
80 if i == 1
81 val[m] = val[m] - 1.
82 end
83 if i == dim1
84 val[m] = val[m] - 1.
85 end
86 if j == 1
87 val[m] = val[m] - 1.
88 end
89 if j == dim2
90 val[m] = val[m] - 1.
91 end
92 if k == 1
93 val[m] = val[m] - 1.
94 end
95 if k == dim3
96 val[m] = val[m] - 1.
97 end
98 val[m] = val[m] * 8.
99 m = m + 1
100 if i < dim1
101 row[m] = i + dim1 * (j-1) + dim1 * dim2 * (k-1) + 1
102 col[m] = (i-1) + dim1 * (j-1) + dim1 * dim2 * (k-1) + 1
103 val[m] = - 1.
104 m = m + 1
105 end
106 if j < dim2
107 row[m] = (i-1) + dim1 * j + dim1 * dim2 * (k-1) + 1
108 col[m] = (i-1) + dim1 * (j-1) + dim1 * dim2 * (k-1) + 1
109 val[m] = - 1.
110 m = m + 1
111 end
112 if k < dim3
113 row[m] = (i-1) + dim1 * (j-1) + dim1 * dim2 * k + 1
114 col[m] = (i-1) + dim1 * (j-1) + dim1 * dim2 * (k-1) + 1
115 val[m] = -1.
116 m = m + 1
117 end
118 end
119 end
120end
121
122if m != nnz+1
123 println( "m ", m, "nnz ", nnz )
124end
125
126A2 = spm.spmatrix_t( zero )
127A2ptr = Ptr{spm.spmatrix_t}( pointer_from_objref(A2) )
128rc = spm.spmCheckAndCorrect( Aptr, A2ptr )
129
130if rc != 0
131 spm.spmExit( Aptr )
132 clear!( :A )
133 clear!( :Aptr )
134 A = A2
135 Aptr = A2ptr
136end
137
138spm.spmPrintInfo( Aptr, my_stdout )
139
140# 2- The right hand side
141
142nrhs = 10
143x0 = zeros( Cdouble, ( A.nexp, nrhs ) )
144b = zeros( Cdouble, ( A.nexp, nrhs ) )
145x = zeros( Cdouble, ( A.nexp, nrhs ) )
146
147spm.spmGenRHS( spm.SpmRhsRndX, nrhs, Aptr, x0, n, b, n )
148
149# Copy x0 into x for backup
150x = x0
151
152# Check that A * x = b
153eps = 1.e-15 # Set to 1e-7 for single precision
154spm.spmCheckAxb( eps, nrhs, Aptr, x, n, b, n, x0, n )
155
156spm.spmExit( Aptr )
157
158clear!( :A )
159clear!( :Aptr )
160clear!( :b )
161clear!( :x )
162clear!( :x0 )
163
164#=
165 @endcode
166=#