---
title: "Monte carlo's simulation for Fisher's exact test on GPU"
output:
  html_document: default
  pdf_document: default
---

<!--
%\VignetteEngine{knitr::knitr}
%\VignetteIndexEntry{Fisher's exact test on GPU}
-->

```{r packages, results='hide', message=FALSE}
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."
```{r  teaTasting, eval=T}
TeaTasting <-matrix(c(3, 1, 1, 3),nrow=2)
TeaTasting

## using R's fisher.test()
fisher.test(TeaTasting)$p.value
```

```{r teaTasting on GPU, eval=T}
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.")
}

```









