tests/R-tests/TestCudaStreams.R

##########################################################################
# Copyright (c) 2023, King Abdullah University of Science and Technology
# All rights reserved.
# MPCR is an R package provided by the STSDS group at KAUST
##########################################################################


library("MPCR")

paste("---------------------------------------------- Linear Algebra --------------------------------------------------------")
values <- c(3.12393, -1.16854, -0.304408, -2.15901,
            -1.16854, 1.86968, 1.04094, 1.35925, -0.304408,
            1.04094, 4.43374, 1.21072, -2.15901, 1.35925, 1.21072, 5.57265)


x <- as.MPCR(values, nrow = 4, ncol = 4, precision = "single", placement = "CPU")
y <- as.MPCR(values, nrow = 4, ncol = 4, precision = "single", placement = "CPU")


paste("X and Y values")
x$PrintValues()
y$PrintValues()

# Perform all the upcoming operation on GPU, if supported.
MPCR.SetOperationPlacement("default", "GPU")
cat("----------------------- CrossProduct C=XY --------------------\n")
# Data will be transfered automatically to GPU to be able to perform the operation
# on GPU
MPCR.CreateRunContext("gpu1")
MPCR.SetRunMode("gpu1", "async")

MPCR.CreateRunContext("gpu2")
MPCR.SetRunMode("gpu2", "sync")

crossproduct_validate <- crossprod(x, y)
cat("validation result \n")
crossproduct_validate$PrintValues()
cat("\n")

MPCR.SetOperationContext("gpu1")
MPCR.SetOperationPlacement("gpu1", "GPU")

crossproduct <- crossprod(x, y)
MPCR.SyncContext("gpu1")

MPCR.SetOperationContext("gpu2")
MPCR.SetOperationPlacement("gpu2", "GPU")
cat("asynchronous result after synchronization \n")
crossproduct$PrintValues()
cat("\n")

Try the MPCR package in your browser

Any scripts or data that you put into this service are public.

MPCR documentation built on Aug. 23, 2026, 5:10 p.m.