Skip to content

Commit

Permalink
Merge pull request #135 from Samyssmile/feature/cudamatrixarithmetic-…
Browse files Browse the repository at this point in the history
…singleton

Change CudaMatrixArithmetic implementation to Singleton pattern
  • Loading branch information
Samyssmile authored Nov 26, 2023
2 parents 3244e05 + 03577a6 commit fff8906
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import org.slf4j.LoggerFactory;

public class CudaMatrixArithmetic implements IMatrixArithmetic {

private static CudaMatrixArithmetic instance;
private static final Logger LOG = LoggerFactory.getLogger(CudaMatrixArithmetic.class);
private final IMatrixProduct matrixProduct;
private final IMatrixVectorProduct matrixVectorProduct;

public CudaMatrixArithmetic() {
private CudaMatrixArithmetic() {
if (!isCudaAvailable()) {
this.matrixProduct = new MatrixProduct();
this.matrixVectorProduct = new MatrixVectorProduct();
Expand Down Expand Up @@ -88,4 +88,10 @@ public double[] multiply(double[][] matrix, double[] vector) {
}
return matrixVectorProduct.multiply(matrix, vector);
}
public static CudaMatrixArithmetic getInstance() {
if(instance == null) {
instance = new CudaMatrixArithmetic();
}
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CudaMatrixMatrixArithmeticTest {

@BeforeAll
static void setUp() {
cudaArithmetic = new CudaMatrixArithmetic();
cudaArithmetic = CudaMatrixArithmetic.getInstance();
}

@Test
Expand Down Expand Up @@ -94,7 +94,7 @@ public void shouldMultiplyWithZeroMatrix() {
{0, 0}
};

IMatrixArithmetic cudaMatrixMultiplication = new CudaMatrixArithmetic();
IMatrixArithmetic cudaMatrixMultiplication = CudaMatrixArithmetic.getInstance();
double[][] result = cudaMatrixMultiplication.multiply(matrixA, matrixB);

assertArrayEquals(expected, result);
Expand Down

0 comments on commit fff8906

Please sign in to comment.