Skip to content

Commit 0411194

Browse files
committed
add warning when coordinates data equals half system memory #50
1 parent 74cbb14 commit 0411194

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

fidimag/common/cuboid_mesh.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
in the innermost loop, and the z-axis in the outermost loop!
2727
2828
"""
29+
import os
2930
import numpy as np
3031
from textwrap import dedent
3132
from six.moves import range
@@ -69,6 +70,7 @@ def __init__(self, dx=1, dy=1, dz=1, nx=1, ny=1, nz=1, x0=0, y0=0, z0=0,
6970
self.n = nx * ny * nz # total number of cells
7071
self.nxy = nx * ny # number of cells in the x-y plane
7172
self.size = (nx, ny, nz)
73+
self.check_size()
7274

7375
self.mesh_type = "cuboid"
7476
self.unit_length = unit_length
@@ -258,3 +260,25 @@ def vertices(self, origin=(0, 0, 0)):
258260
grid[i] = (x, y, z)
259261
i += 1
260262
return grid
263+
264+
def check_size(self, system_memory_fake_for_testing=None):
265+
"""
266+
Provide an estimate of how much system memory is needed and warn accordingly.
267+
268+
"""
269+
bytes_per_float_numpy = 8
270+
size_coordinates_bytes = self.nx * self.ny * self.nz * 3 * bytes_per_float_numpy
271+
size_coordinates_GiB = size_coordinates_bytes / (1024. ** 3)
272+
273+
if system_memory_fake_for_testing is None:
274+
mem_bytes = os.sysconf('SC_PAGE_SIZE') * os.sysconf('SC_PHYS_PAGES')
275+
mem_GiB = mem_bytes / (1024. ** 3)
276+
else:
277+
mem_GiB = system_memory_fake_for_testing
278+
279+
if 2 * size_coordinates_GiB > mem_GiB:
280+
# print because no logging yet
281+
print "Warning! Size of mesh coordinates i {} GiB.".format(size_coordinates_GiB)
282+
print "You have {} GiB system memory. Possible halt.".format(mem_GiB)
283+
return 1
284+
return 0

fidimag/common/cuboid_mesh_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,8 @@ def test_vector_field_shape():
242242
expected_shape_for_vector_field = (expected_nb_cells, 3)
243243
assert mesh.vector_shape() == expected_shape_for_vector_field
244244
m = np.zeros(mesh.vector_shape()) # usage example
245+
246+
247+
def test_check_size():
248+
mesh = CuboidMesh(1, 1, 1, 2, 3, 4)
249+
assert 1 == mesh.check_size(system_memory_fake_for_testing=0)

0 commit comments

Comments
 (0)