Skip to content

Commit cac61c1

Browse files
authored
Merge pull request #62 from logicabrity/large-meshes
add warning when coordinates data equals half system memory #50
2 parents 74cbb14 + 779a65f commit cac61c1

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

fidimag/common/cuboid_mesh.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
in the innermost loop, and the z-axis in the outermost loop!
2727
2828
"""
29+
from __future__ import print_function
30+
import os
2931
import numpy as np
3032
from textwrap import dedent
3133
from six.moves import range
@@ -69,6 +71,7 @@ def __init__(self, dx=1, dy=1, dz=1, nx=1, ny=1, nz=1, x0=0, y0=0, z0=0,
6971
self.n = nx * ny * nz # total number of cells
7072
self.nxy = nx * ny # number of cells in the x-y plane
7173
self.size = (nx, ny, nz)
74+
self.check_size()
7275

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