Monte carlo’s simulation for Fisher’s exact test on GPU

library("clrng")

Example

This example table is small in dimension and size of values. We do the simulation on GPU just for demonstration and test, don’t really need a simulation.. See more tests on real data examples on section 4 of the paper “Ruoyong Xu, Patrick Brown, Pierre L’Ecuyer (2021). A tool set for random number generation on GPUs in R.”

TeaTasting <-matrix(c(3, 1, 1, 3),nrow=2)
TeaTasting
##      [,1] [,2]
## [1,]    3    1
## [2,]    1    3
## using R's fisher.test()
fisher.test(TeaTasting)$p.value
## [1] 0.4857143
if (detectGPUs()) {
  setContext(grep("gpu", listContexts()$device_type)[1])
  
  ## get current device name
  gpuInfo()$deviceName
  ## using clrng's fisher.sim()
  ## check the size of work items and GPU precision type at the moment
  getOption('clrng.Nglobal')
  getOption('clrng.type')
  
  ## convert the data table to be on GPU
  TeaTastingGpu<-gpuR::vclMatrix(TeaTasting,type="integer")
  
  ## choose the size of Nglobal and create 16*64 streams
  options(clrng.Nglobal=c(16,64))
  streams <- gpuR::vclMatrix(clrng::createStreamsCpu())
  
  ## perform 100000 fisher's simulation on GPU and return all test statistics
  result<-clrng::fisher.sim(TeaTastingGpu, N=1e5, streams=streams,returnStatistics=TRUE)
  print(result)
  
  ## show some simulation results
  result$threshold
  as.vector(result$sim)[10:20]
  length(result$sim)
  
} else {
  message("No GPU detected. Skipping GPU-dependent code.")
}
## 
## 	Fisher's Exact Test for Count Data with simulated p-value (based on
## 	100352 replicates)
## 
## data:  TeaTastingGpu
## p-value = 0.4879
## [1] 100352