use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class EndlessWorkspaceTests method endlessTestSerDe1.
@Test
public void endlessTestSerDe1() throws Exception {
INDArray features = Nd4j.create(32, 3, 224, 224);
INDArray labels = Nd4j.create(32, 200);
File tmp = File.createTempFile("12dadsad", "dsdasds");
float[] array = new float[33 * 3 * 224 * 224];
DataSet ds = new DataSet(features, labels);
ds.save(tmp);
WorkspaceConfiguration wsConf = WorkspaceConfiguration.builder().initialSize(0).policyLearning(LearningPolicy.FIRST_LOOP).build();
while (true) {
try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace(wsConf, "serde")) {
/*
try (FileOutputStream fos = new FileOutputStream(tmp); BufferedOutputStream bos = new BufferedOutputStream(fos)) {
SerializationUtils.serialize(array, fos);
}
try (FileInputStream fis = new FileInputStream(tmp); BufferedInputStream bis = new BufferedInputStream(fis)) {
long time1 = System.currentTimeMillis();
float[] arrayR = (float[]) SerializationUtils.deserialize(bis);
long time2 = System.currentTimeMillis();
log.info("Load time: {}", time2 - time1);
}
*/
long time1 = System.currentTimeMillis();
ds.load(tmp);
long time2 = System.currentTimeMillis();
log.info("Load time: {}", time2 - time1);
}
}
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class EndlessWorkspaceTests method endlessTest4.
@Test
public void endlessTest4() throws Exception {
Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(WorkspaceConfiguration.builder().initialSize(100 * 1024L * 1024L).build());
while (true) {
try (MemoryWorkspace workspace1 = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS_1")) {
for (int i = 0; i < 1000; i++) {
INDArray array = Nd4j.createUninitialized(RandomUtils.nextInt(1, 50), RandomUtils.nextInt(1, 50));
INDArray mean = array.max(1);
}
for (int i = 0; i < 1000; i++) {
INDArray array = Nd4j.createUninitialized(RandomUtils.nextInt(1, 100));
array.maxNumber().doubleValue();
}
}
}
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class EndlessWorkspaceTests method endlessTest2.
/**
* This test checks for allocation from workspace AND spills
* @throws Exception
*/
@Test
public void endlessTest2() throws Exception {
Nd4j.getWorkspaceManager().setDefaultWorkspaceConfiguration(WorkspaceConfiguration.builder().initialSize(10 * 1024L * 1024L).build());
Nd4j.getMemoryManager().togglePeriodicGc(false);
AtomicLong counter = new AtomicLong(0);
while (true) {
try (MemoryWorkspace workspace = Nd4j.getWorkspaceManager().getAndActivateWorkspace()) {
long time1 = System.nanoTime();
INDArray array = Nd4j.create(2 * 1024 * 1024);
long time2 = System.nanoTime();
array.addi(1.0f);
assertEquals(1.0f, array.meanNumber().floatValue(), 0.1f);
long time3 = System.nanoTime();
INDArray array2 = Nd4j.create(3 * 1024 * 1024);
long time4 = System.nanoTime();
if (counter.incrementAndGet() % 1000 == 0) {
log.info("{} iterations passed... Allocation time: {} vs {} (ns)", counter.get(), time2 - time1, time4 - time3);
System.gc();
}
}
}
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class SpecialWorkspaceTests method testVariableTimeSeries1.
@Test
public void testVariableTimeSeries1() throws Exception {
WorkspaceConfiguration configuration = WorkspaceConfiguration.builder().initialSize(0).overallocationLimit(3.0).policyAllocation(AllocationPolicy.OVERALLOCATE).policySpill(SpillPolicy.EXTERNAL).policyLearning(LearningPolicy.FIRST_LOOP).policyReset(ResetPolicy.ENDOFBUFFER_REACHED).build();
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
Nd4j.create(500);
Nd4j.create(500);
}
Nd4jWorkspace workspace = (Nd4jWorkspace) Nd4j.getWorkspaceManager().getWorkspaceForCurrentThread("WS1");
workspace.enableDebug(true);
assertEquals(0, workspace.getStepNumber());
long requiredMemory = 1000 * Nd4j.sizeOfDataType();
long shiftedSize = ((long) (requiredMemory * 1.3)) + (8 - (((long) (requiredMemory * 1.3)) % 8));
assertEquals(requiredMemory, workspace.getSpilledSize());
assertEquals(shiftedSize, workspace.getInitialBlockSize());
assertEquals(workspace.getInitialBlockSize() * 4, workspace.getCurrentSize());
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace("WS1")) {
Nd4j.create(2000);
}
assertEquals(0, workspace.getStepNumber());
assertEquals(1000 * Nd4j.sizeOfDataType(), workspace.getSpilledSize());
assertEquals(2000 * Nd4j.sizeOfDataType(), workspace.getPinnedSize());
assertEquals(0, workspace.getDeviceOffset());
// FIXME: fix this!
// assertEquals(0, workspace.getHostOffset());
assertEquals(0, workspace.getThisCycleAllocations());
log.info("------------------");
assertEquals(1, workspace.getNumberOfPinnedAllocations());
for (int e = 0; e < 4; e++) {
for (int i = 0; i < 4; i++) {
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
Nd4j.create(500);
Nd4j.create(500);
}
assertEquals("Failed on iteration " + i, (i + 1) * workspace.getInitialBlockSize(), workspace.getDeviceOffset());
}
if (e >= 2) {
assertEquals("Failed on iteration " + e, 0, workspace.getNumberOfPinnedAllocations());
} else {
assertEquals("Failed on iteration " + e, 1, workspace.getNumberOfPinnedAllocations());
}
}
assertEquals(0, workspace.getSpilledSize());
assertEquals(0, workspace.getPinnedSize());
assertEquals(0, workspace.getNumberOfPinnedAllocations());
assertEquals(0, workspace.getNumberOfExternalAllocations());
log.info("Workspace state after first block: ---------------------------------------------------------");
Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread();
log.info("--------------------------------------------------------------------------------------------");
// we just do huge loop now, with pinned stuff in it
for (int i = 0; i < 100; i++) {
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
Nd4j.create(500);
Nd4j.create(500);
Nd4j.create(500);
assertEquals(1500 * Nd4j.sizeOfDataType(), workspace.getThisCycleAllocations());
}
}
assertEquals(0, workspace.getSpilledSize());
assertNotEquals(0, workspace.getPinnedSize());
assertNotEquals(0, workspace.getNumberOfPinnedAllocations());
assertEquals(0, workspace.getNumberOfExternalAllocations());
// and we do another clean loo, without pinned stuff in it, to ensure all pinned allocates are gone
for (int i = 0; i < 100; i++) {
try (MemoryWorkspace ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(configuration, "WS1")) {
Nd4j.create(500);
Nd4j.create(500);
}
}
assertEquals(0, workspace.getSpilledSize());
assertEquals(0, workspace.getPinnedSize());
assertEquals(0, workspace.getNumberOfPinnedAllocations());
assertEquals(0, workspace.getNumberOfExternalAllocations());
log.info("Workspace state after second block: ---------------------------------------------------------");
Nd4j.getWorkspaceManager().printAllocationStatisticsForCurrentThread();
}
use of org.nd4j.linalg.api.memory.MemoryWorkspace in project nd4j by deeplearning4j.
the class CpuWorkspaceManager method createNewWorkspace.
@Override
public MemoryWorkspace createNewWorkspace(@NonNull WorkspaceConfiguration configuration, @NonNull String id) {
ensureThreadExistense();
MemoryWorkspace workspace = new CpuWorkspace(configuration, id);
backingMap.get().put(id, workspace);
pickReference(workspace);
return workspace;
}
Aggregations