-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path061_2D_Inf_neg_logL_CAR_GPU_CPU.R
445 lines (311 loc) · 11.1 KB
/
061_2D_Inf_neg_logL_CAR_GPU_CPU.R
1
2
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#=============
# 18 Apr. 2024
#=============
# Aim:
# GPU version of 055_2D_Inf_neg_logL_CAR
# Methods:
# source("055_2D_Inf_neg_logL_CAR_2D.R")
# GPUmatrix
# df: 046d
#==============
# GPU settings
#==============
#------
# torch
#------
# Set the library path to the desired directory
.libPaths("/bask/projects/v/vjgo8416-xchen")
# Load the torch library
library(torch)
#-----------
# GPUmatrix
#------------
#install.packages("GPUmatrix", lib="/bask/projects/v/vjgo8416-xchen")
.libPaths("/bask/projects/v/vjgo8416-xchen")
library(GPUmatrix)
system("nvidia-smi")
require(Matrix)
#============================
# multi-CPU core pre-settings
#============================
#install.packages("optimParallel")
.libPaths("/bask/projects/v/vjgo8416-xchen")
library(optimParallel)
#==============
# Pre-settings
#==============
#-----
# df
#-----
df_2D_TW_CAMS <- readRDS("df_2D_TW_CAMS.rds")
#head(df_2D_TW_CAMS)
#str(df_2D_TW_CAMS)
# data.frame': 200 obs. of 12 variables
#---------
# data str
#---------
hierarchy_data_CAMS <- data.frame(
node_id = c(1, 2, 3, 4, 5, 5),
par_id = c(NA, 1, 2, 3, c(4, 1))
)
p = 5
data_str <- hierarchy_data_CAMS
#-----
# pars
#-----
source("Fn_para_mat_construct.R")
all_pars_lst_CAR_2D_CMS <- All_paras_CAR_2D(p = 5, data = hierarchy_data_CAMS)
all_pars_lst <- all_pars_lst_CAR_2D_CMS
#-------
# set some ini for theta to run the neg_logL
#-------
#Vals <- c(0.1, 0.2, 0.1, 0.1, 0.1,
# 0.1, 0.1, 0.1, 0.1, 0.1,
# rep(0.2, 5),
# rep(1, 5)) # w/o tau2s
#theta <- c(Vals, rep(0.2, p))
#----------
# 2D coords
#----------
ds <- 0.1
s <- seq(-10 + ds/2, 10 - ds/2, by = ds)
crds <- cbind(s, s)
# nrow(crds) [1] 200
#----------------------------------------
# Construct displacement matrix (DSP_mat)
#----------------------------------------
# Aim:
# for TST12 function to construct shft dist matrix
source("Fn_make_DSP_mat.R")
DSP <- make_DSP_mat(crds = crds)
#str(DSP[, , 1]) # num [1:200, 1:200]
#str(DSP[, , 2])
#---------------------------
# Construct distance matrix
#---------------------------
# Aim:
# for H_adj and phi for UniCAR
DIST <- as.matrix(dist(crds, diag = T, upper = T))
Nb_radius <- 0.6 # lag-4
H_adj <- matrix(as.numeric(abs(DIST) < Nb_radius), nrow(DIST), nrow(DIST))
diag(H_adj) <- 0
spec <- eigen(H_adj, symmetric = T, only.values = T)$val
phi <- 1/max(abs(spec)) # 0.1251127
phi <- trunc(phi * 100)/100 # [1] 0.12
#------
# try Nb_radius <- 8 50%
#------
# Expect: more data left in H_adj
# more diverse the eigen vectors and eig value
# smaller 1/max(abs(eig))
# smaller phi
## see:Investigate_phi_Nb_radius.R
#==================
# neg_logL function
#==================
neg_logL_CAR_2D_GPU <- function(theta, p, data_str, all_pars_lst,
dsp_lon_mat, dsp_lat_mat, b = "Tri-Wave",
phi, H_adj, df){
#source("Fn_TST12_SG_SGInv_CAR_2D_GPU.R")
#source("Fn_I_sparse.R")
#source("Fn_chol_inv_gpu.R") # input a gpumatrix, return chol inv
# connect each component of theta to all_pars_lst
# to incoporate each theta component into the neg log L function
theta_indx <- 1
for (lst in 1:length(all_pars_lst)){
for (i in 1:nrow(all_pars_lst[[lst]])){
for (j in 1:ncol(all_pars_lst[[lst]])){
if (is.na(all_pars_lst[[lst]][i, j])){
all_pars_lst[[lst]][i, j] <- theta[theta_indx]
theta_indx <- theta_indx + 1
}
}
}
}
# construct SIGMA_Y, SIGMA_Y_inv for process Y
SG_SG_inv_Y_gpu <- TST12_SG_SGInv_CAR_2D_GPU(p = p, data = data_str,
A_mat = all_pars_lst[[1]],
dsp_lon_mat = dsp_lon_mat,
dsp_lat_mat = dsp_lat_mat,
dlt_lon_mat = all_pars_lst[[2]],
dlt_lat_mat = all_pars_lst[[3]],
b = b, phi = phi,
H_adj = H_adj,
sig2_mat = all_pars_lst[[4]],
reg_ini = 1e-9, thres_ini = 1e-3)
SG_Y_gpu <- SG_SG_inv_Y_gpu$SIGMA_gpu
SG_Y_inv_gpu <- SG_SG_inv_Y_gpu$SIGMA_inv_gpu
# calculate SG_Ng
## 1st calcuate the # of parameters accumulated so far,
# so can connect theta components on top of current index
# with measurement error tau2
#source("Fn_para_mat_construct.R")
all_pars_lst <- All_paras_CAR_2D(p = p, data = data_str)
# for assign NA
SUM <- 0
for (i in 1:length(all_pars_lst)){
s <- sum(is.na(all_pars_lst[[i]]))
SUM <- SUM + s
}
# SUM 33
# tau2 diag matrix
#tau2_mat <- diag(theta[SUM+1], theta[SUM+2], ..., theta[SUM+p] )
THETA <- c()
for(i in 1:p){
THETA <- c(THETA, theta[SUM + i])
}
tau2_mat <- diag(THETA)
tau2_mat_gpu <- as.gpu.matrix(tau2_mat, device = "cuda")
# total # of locations of univariate process
n1 <- nrow(df) # 200
I_sp_mat <- I_sparse(size = n1, value = 1)
I_sp_gpu <- as.gpu.matrix(as.matrix(I_sp_mat), device = "cuda")
SG_Ng_gpu <- tau2_mat_gpu %x% I_sp_gpu
#SG_Ng <- kronecker(tau2_mat, I_sp_mat)
SG_Ng_inv_gpu <- solve(SG_Ng_gpu)
#str(SG_Ng_ft) # num [1:450]
# observation Z
SG_Z_gpu = SG_Y_gpu + SG_Ng_gpu
# SG_Z_inv = SG_Ng_inv - SG_Ng_inv(SG_Y_inv +SG_Ng_inv)^{-1}SG_Ng_inv
SG_Y_Ng_gpu <- SG_Y_inv_gpu + SG_Ng_inv_gpu
#SG_Y_Ng_inv <- chol2inv(chol(SG_Y_Ng))
SG_Y_Ng_inv_gpu <- chol_inv_gpu(SG_Y_Ng_gpu)
SG_Z_inv_gpu <- SG_Ng_inv_gpu - SG_Ng_inv_gpu %*% SG_Y_Ng_inv_gpu %*% SG_Ng_inv_gpu
# log_det(SG_Z)
#source("Fn_log_det.R")
#chol_SG_Z <- chol(SG_Z)
#log_SG_Z_det <- log_det(chol_SG_Z)
log_SG_Z_det_gpu <- log(det(SG_Z_gpu))
# construct joint Z, stack each Zi in df
Z <- c()
for (i in 1:p) {
Z <- c(Z, df[[paste0("Z", i)]])
}
# str(Z)# num [1:1200] or use Fn_Stack_Z
Z_gpu <- as.gpu.matrix(Z, device = "cuda")
# neg_logL
L <- length(Z_gpu) # different from n1 = length(Z1)
neg_logL_gpu <- - (- (L/2) * log(2*pi) - 1/2 * log_SG_Z_det_gpu -
1/2 * t(Z_gpu) %*% SG_Z_inv_gpu %*% Z_gpu) # a 1 by 1 matrix on gpu
neg_logL <- as.numeric(neg_logL_gpu, device = "cpu") # a scalar on cpu
# return scalar
return(neg_logL)
}
#==================================
# Test neg_logL_CAR_2D_GPU function (success!)
#==================================
#neg_logL_CAR_2D_GPU(theta = theta, p = p, data_str = hierarchy_data_CAMS,
# all_pars_lst = all_pars_lst_CAR_2D_CMS,
# dsp_lon_mat = DSP[, , 1],
# dsp_lat_mat = DSP[, , 2],
# b = "Tri-Wave",
# phi = phi, H_adj = H_adj,
# df = df_2D_TW_CAMS)
# [1] 1460.098
#========
# Optim
#========
source("Fn_para_mat_construct.R")
all_pars_lst_CAR_2D_CMS <- All_paras_CAR_2D(p = 5, data = hierarchy_data_CAMS)
all_pars_lst <- all_pars_lst_CAR_2D_CMS
#-----------
# ini values
#-----------
ini <- c(0.2, 0.1, 0.1, 0.5) # A, dlt_lon, dlt_lat, sig2
Vals <- c()
for (i in 1:length(all_pars_lst)){
value <- rep(ini[i], sum(is.na(all_pars_lst[[i]])))
Vals <- c(Vals, value)
}
all_ini_Vals <- c(Vals, rep(0.1, p)) # with tau2s
#---------------------------------
## lower bound for each parameters
#---------------------------------
# NA: no lower bound
lower_bound <- c(rep(NA, sum(is.na(all_pars_lst[[1]]))), # A
rep(0.05, sum(is.na(all_pars_lst[[2]]))), # dlt_lon
rep(0.05, sum(is.na(all_pars_lst[[3]]))), # dlt_lat
rep(0.001, sum(is.na(all_pars_lst[[4]]))), # sig2
rep(0.001, p)) # tau2
#=============================
# multi-core parallel settings
#=============================
# Create a cluster
cl <- makeCluster(4)
# Set the created cluster as the default for parallel computation
setDefaultCluster(cl = cl)
# Specify the library path on each worker
clusterEvalQ(cl, {
.libPaths("/bask/projects/v/vjgo8416-xchen")
})
# Load the packages on each worker
clusterEvalQ(cl, {
library(torch)
library(GPUmatrix)
library(optimParallel)
})
# Load the core R package (no need to specify the path)
clusterEvalQ(cl, {
library(Matrix)
})
# Source functions here to the current env
source("Fn_Check_par_node.R")
source("Fn_I_sparse.R")
source("Fn_chol_inv_gpu.R") # input a gpumatrix, return chol inv
source("Fn_shft_dist_mat.R") # construct shifted distance matrix using shft displacement for b function
source("Fn_Waves.R")
source("Fn_Wendland_32.R") # R = 0.5
source("Fn_check_set_SpNorm_Reg.R") # SpN + tune regularize number
source("Fn_forceSym_GPU.R") # forceSym on GPU
source("Fn_Tst_sym_pd_GPU.R") # GPU version
source("Fn_Thres_tune_cov_GPU.R") # thresholding SIGMA_inv and return SIGMA and SIGMA_inv on GPU
source("Fn_para_mat_construct.R")
source("Fn_TST12_SG_SGInv_CAR_2D_GPU.R")
# Save each function as a variable in the current Env
Check_par_node <- get("Check_par_node")
I_sparse <- get("I_sparse")
chol_inv_gpu <- get("chol_inv_gpu")
Shft_dst_mat <- get("Shft_dst_mat")
TriWave_2D <- get("TriWave_2D")
WendLd32_2D <- get("WendLd32_2D")
check_set_SpNorm_Reg <- get("check_set_SpNorm_Reg")
forceSym_gpu <- get("forceSym_gpu")
Tst_sym_pd_gpu <- get("Tst_sym_pd_gpu")
spress_cov <- get("spress_cov")
check_pd_gpu <- get("check_pd_gpu")
Thres_tune_cov_gpu <- get("Thres_tune_cov_gpu")
TST12_SG_SGInv_CAR_2D_GPU <- get("TST12_SG_SGInv_CAR_2D_GPU")
Para_A_mat <- get("Para_A_mat")
Para_Dlt_lon_mat <- get("Para_Dlt_lon_mat")
Para_Dlt_lat_mat <- get("Para_Dlt_lat_mat")
Para_sig2_mat <- get("Para_sig2_mat")
All_paras_CAR_2D <- get("All_paras_CAR_2D")
# Export the variables to the cluster workers
clusterExport(cl, c("Check_par_node", "I_sparse", "chol_inv_gpu",
"TriWave_2D", "WendLd32_2D", "Tst_sym_pd_gpu",
"check_set_SpNorm_Reg", "spress_cov", "check_pd_gpu",
"Thres_tune_cov_gpu", "Shft_dst_mat", "forceSym_gpu",
"TST12_SG_SGInv_CAR_2D_GPU", "All_paras_CAR_2D", "Para_A_mat",
"Para_Dlt_lon_mat", "Para_Dlt_lat_mat", "Para_sig2_mat"))
# export each variable name to each worker
clusterExport(cl, c("all_ini_Vals", "p",
"hierarchy_data_CAMS", "all_pars_lst_CAR_2D_CMS",
"DSP", "phi", "H_adj", "df_2D_TW_CAMS", "lower_bound"))
#---------
# Tri-Wave
#---------
optm_pars_CAR_2D_TW_GPU_CPU <- optimParallel(par = all_ini_Vals, # ini guess
fn = neg_logL_CAR_2D_GPU,
p = p, data_str = hierarchy_data_CAMS,
all_pars_lst = all_pars_lst_CAR_2D_CMS,
dsp_lon_mat = DSP[, , 1],
dsp_lat_mat = DSP[, , 2],
b = "Tri-Wave",
phi = phi, H_adj = H_adj,
df = df_2D_TW_CAMS,
method = "L-BFGS-B",
lower = lower_bound,
control = list(maxit = 1,
factr=.05/.Machine$double.eps))
stopCluster(cl)
optm_pars_CAR_2D_TW_GPU_CPU