# PCA on GPU - Insufficient memory

**URL:** <https://community.konduit.ai/t/pca-on-gpu-insufficient-memory/798>\
**Category:** ND4J\
**Created:** [August 19, 2020, 9:07pm UTC](https://community.konduit.ai/t/pca-on-gpu-insufficient-memory/798 "2020-08-19T21:07:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sogawa-sps](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sogawa-sps/32/44_2.png) [@sogawa-sps](https://community.konduit.ai/u/sogawa-sps)\
**Post date:** [August 19, 2020, 9:07pm UTC](https://community.konduit.ai/t/pca-on-gpu-insufficient-memory/798/1 "2020-08-19T21:07:32Z")

</div>

Hi All,

I tried to perform PCA factorization on relatively small data set using GPU but ran into error. Code sample:

```
INDArray data = Nd4j.rand(1000,31000);
PCA.pca_factor(data, 50, false);

```

Output:

```
[main] INFO org.nd4j.linalg.factory.Nd4jBackend - Loaded [JCublasBackend] backend
[main] INFO org.nd4j.nativeblas.NativeOpsHolder - Number of threads used for linear algebra: 32
[main] INFO org.nd4j.linalg.api.ops.executioner.DefaultOpExecutioner - Backend used: [CUDA]; OS: [Windows 10]
[main] INFO org.nd4j.linalg.api.ops.executioner.DefaultOpExecutioner - Cores: [4]; Memory: [7.1GB];
[main] INFO org.nd4j.linalg.api.ops.executioner.DefaultOpExecutioner - Blas vendor: [CUBLAS]
[main] INFO org.nd4j.linalg.jcublas.JCublasBackend - ND4J CUDA build version: 10.2.89
[main] INFO org.nd4j.linalg.jcublas.JCublasBackend - CUDA device 0: [GeForce GTX 1060 6GB]; cc: [6.1]; Total memory: [6442450944]
Exception in thread "main" java.lang.RuntimeException: Allocation failed: [[DEVICE] allocation failed; Error code: [2]]
	at org.nd4j.nativeblas.OpaqueDataBuffer.allocateDataBuffer(OpaqueDataBuffer.java:79)
	at org.nd4j.linalg.jcublas.buffer.BaseCudaDataBuffer.initPointers(BaseCudaDataBuffer.java:389)
	...

```

And while it’s not completely clear from error message it looks like that memory is the reason.

![2020-08-19-ND4J-PCA](https://canada1.discourse-cdn.com/flex035/uploads/konduit/original/1X/2afec51ffdb39731d8bb721d6684f05e3af6ce4c.png)

CPU backed works just fine.

Could somebody please advise is there a way run PCA on GPU in this case? Does it make a sense to use that shared memory and how to do it if so? How to tackle such cases generally?

---

<div class="post-metadata">

**Author:** ![agibsonccc](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/agibsonccc/32/697_2.png) [@agibsonccc](https://community.konduit.ai/u/agibsonccc)\
**Post date:** [August 21, 2020, 4:28am UTC](https://community.konduit.ai/t/pca-on-gpu-insufficient-memory/798/2 "2020-08-21T04:28:52Z")

</div>

> [@sogawa-sps](#):
>
> PCA on GPU in this case? Does it make a sense to use that shared memory and how to do it if so? H

Hi, that doesn’t seem right. Do you have a test case we can see?

---

<div class="post-metadata">

**Author:** ![sogawa-sps](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.konduit.ai/sogawa-sps/32/44_2.png) [@sogawa-sps](https://community.konduit.ai/u/sogawa-sps)\
**Post date:** [August 21, 2020, 11:53pm UTC](https://community.konduit.ai/t/pca-on-gpu-insufficient-memory/798/3 "2020-08-21T23:53:43Z")

</div>

Hi!

It reproduces on any INDArray big enough like in code snippet from the post above (ready to run sample: [GitHub - sogawa-sps/testnd4j](https://github.com/sogawa-sps/testnd4j)). In my real world app I have a sparse matrix (density less then 0.001) but issue reproduces on dense randomly filled matrix in the same way.
