[cig-commits] r6591 - short/3D/PyLith/branches/pylith-0.8/pylith3d/module

leif at geodynamics.org leif at geodynamics.org
Tue Apr 17 11:47:48 PDT 2007


Author: leif
Date: 2007-04-17 11:47:48 -0700 (Tue, 17 Apr 2007)
New Revision: 6591

Modified:
   short/3D/PyLith/branches/pylith-0.8/pylith3d/module/PyLithLib.pyx
   short/3D/PyLith/branches/pylith-0.8/pylith3d/module/array.pyx
Log:
Re-implemented memory-usage-report feature, but this time with only a
few lines of code.  Factored-out Python-list-to-C-array code.  Added
__get__() methods so that public list/array properties are readable as
well as writeable.


Modified: short/3D/PyLith/branches/pylith-0.8/pylith3d/module/PyLithLib.pyx
===================================================================
--- short/3D/PyLith/branches/pylith-0.8/pylith3d/module/PyLithLib.pyx	2007-04-17 18:44:39 UTC (rev 6590)
+++ short/3D/PyLith/branches/pylith-0.8/pylith3d/module/PyLithLib.pyx	2007-04-17 18:47:48 UTC (rev 6591)
@@ -136,35 +136,39 @@
 
     property grav: # gravityX, gravityY, gravityZ in m/(s*s)
         def __set__(self, gravity):
-            cdef int dim
-            dim = sizeof(self._grav)/sizeof(self._grav[0])
-            assert(len(gravity) == dim)
-            for i from 0 <= i < dim:
-                self._grav[i] = gravity[i]
+            setDoubleArrayFromList(&self._grav[0],
+                                   sizeof(self._grav)/sizeof(self._grav[0]),
+                                   gravity)
+        def __get__(self):
+            return listFromDoubleArray(&self._grav[0],
+                                       sizeof(self._grav)/sizeof(self._grav[0]))
 
     property prscal: # prestressScale{Xx,Yy,Zz,Xy,Xz,Yz}
         def __set__(self, prestressScale):
-            cdef int dim
-            dim = sizeof(self._prscal)/sizeof(self._prscal[0])
-            assert(len(prestressScale) == dim)
-            for i from 0 <= i < dim:
-                self._prscal[i] = prestressScale[i]
+            setDoubleArrayFromList(&self._prscal[0],
+                                   sizeof(self._prscal)/sizeof(self._prscal[0]),
+                                   prestressScale)
+        def __get__(self):
+            return listFromDoubleArray(&self._prscal[0],
+                                       sizeof(self._prscal)/sizeof(self._prscal[0]))
 
     property wscal: # winklerScaleX, winklerScaleY, winklerScaleZ
         def __set__(self, winklerScale):
-            cdef int dim
-            dim = sizeof(self._wscal)/sizeof(self._wscal[0])
-            assert(len(winklerScale) == dim)
-            for i from 0 <= i < dim:
-                self._wscal[i] = winklerScale[i]
+            setDoubleArrayFromList(&self._wscal[0],
+                                   sizeof(self._wscal)/sizeof(self._wscal[0]),
+                                   winklerScale)
+        def __get__(self):
+            return listFromDoubleArray(&self._wscal[0],
+                                       sizeof(self._wscal)/sizeof(self._wscal[0]))
     
     property wxscal: # winklerSlipScaleX, winklerSlipScaleY, winklerSlipScaleZ
         def __set__(self, winklerSlipScale):
-            cdef int dim
-            dim = sizeof(self._wxscal)/sizeof(self._wxscal[0])
-            assert(len(winklerSlipScale) == dim)
-            for i from 0 <= i < dim:
-                self._wxscal[i] = winklerSlipScale[i]
+            setDoubleArrayFromList(&self._wxscal[0],
+                                   sizeof(self._wxscal)/sizeof(self._wxscal[0]),
+                                   winklerSlipScale)
+        def __get__(self):
+            return listFromDoubleArray(&self._wxscal[0],
+                                       sizeof(self._wxscal)/sizeof(self._wxscal[0]))
 
     #
     # materialPropertiesInputFile    iii.prop
@@ -174,21 +178,16 @@
     
     property prop:
         def __set__(self, propertyList):
-            cdef int nprops
-            nprops = len(propertyList)
-            self._prop = DoubleArray(nprops)
-            for i from 0 <= i < nprops:
-                self._prop.ptr[i] = propertyList[i]
+            self._prop = DoubleArray(propertyList)
+        def __get__(self):
+            return self._prop.asList()
 
     property infmat:
         def __set__(self, materialModel):
-            cdef int ninfmats
-            ninfmats = len(materialModel)
-            self._infmat = IntArray(ninfmats)
-            for i from 0 <= i < ninfmats:
-                self._infmat.ptr[i] = materialModel[i]
+            self._infmat = IntArray(materialModel)
+        def __get__(self):
+            return self._infmat.asList()
 
-
     # Unit numbers for Fortran I/O.
     cdef public int   kti             # f77StandardInput
     cdef public int   kto             # f77StandardOutput
@@ -2312,11 +2311,10 @@
             
         return
 
+
     cdef interpolatePoints(self, points):
         return self._mesh.interpolatePoints(points)
 
-    cdef viscos_setup(self):
-        return
 
     cdef runSimulation(self):
         # First define all of the lists that maintain variable values.  The
@@ -2332,14 +2330,13 @@
         
         print "Beginning problem solution."
 
-        if False: # Temporarily out-of-order
-            # Output approximate memory usage
-            self.memorySizeMB =0.0
-            self.memorySizeMB=self.memorySize/(1024.0*1024.0)
+        # Output approximate memory usage
+        global memoryUsage
+        memoryUsageMB = float(memoryUsage) / (1024.0 * 1024.0)
+        print
+        print "Approximate memory allocation for f77 arrays: %g MB" % memoryUsageMB
+        print
 
-            print ""
-            print "Approximate memory allocation for f77 arrays (MB): %g" % self.memorySizeMB
-
         # Compute gravitational prestresses, if requeste
         if self.icode == 2 or self.icode == 3:  # elasticSolution or fullSolution
             if self.ipstrs == 1:

Modified: short/3D/PyLith/branches/pylith-0.8/pylith3d/module/array.pyx
===================================================================
--- short/3D/PyLith/branches/pylith-0.8/pylith3d/module/array.pyx	2007-04-17 18:44:39 UTC (rev 6590)
+++ short/3D/PyLith/branches/pylith-0.8/pylith3d/module/array.pyx	2007-04-17 18:47:48 UTC (rev 6591)
@@ -34,58 +34,108 @@
     void free(void *)
 
 
+cdef int memoryUsage
+
+
 cdef class IntArray:
     
     cdef int len
     cdef int *ptr
-    cdef int *_rawptr
     
-    def __new__(self, unsigned int len):
-        self.len = len
-        self._rawptr = <int *>malloc(sizeof(int) * (len + 2))
-        if self._rawptr is NULL:
+    def __new__(self, l):
+        cdef object init
+        cdef long int size
+        if isinstance(l, int):
+            self.len = l
+        else:
+            self.len = len(l)
+            init = l
+        size = sizeof(int) * self.len
+        self.ptr = <int *>malloc(size)
+        if self.ptr is NULL:
             raise MemoryError("malloc failed")
-        self._rawptr[0] = 0xdeadbeef
-        self._rawptr[len + 1] = 0xdeadbeef
-        self.ptr = self._rawptr + 1
+        global memoryUsage
+        memoryUsage = memoryUsage + size
+        if init:
+            for i from 0 <= i < self.len:
+                self.ptr[i] = init[i]
         return
 
     def __dealloc__(self):
-        if (self._rawptr[0] != 0xdeadbeef or
-            self._rawptr[self.len + 1] != 0xdeadbeef):
-            print "memory corrupted"
-        free(self._rawptr)
-        self._rawptr = NULL
+        free(self.ptr)
         self.ptr = NULL
+        global memoryUsage
+        memoryUsage = memoryUsage - (sizeof(int) * self.len)
         return
 
+    cdef asList(self):
+        return listFromIntArray(self.ptr, self.len)
 
+
 cdef class DoubleArray:
     
     cdef int len
     cdef double *ptr
-    cdef double *_rawptr
-    cdef double guard
     
-    def __new__(self, unsigned int len):
-        self.len = len
-        self._rawptr = <double *>malloc(sizeof(double) * (len + 2))
-        if self._rawptr is NULL:
+    def __new__(self, l):
+        cdef object init
+        cdef long int size
+        if isinstance(l, int):
+            self.len = l
+        else:
+            self.len = len(l)
+            init = l
+        size = sizeof(double) * self.len
+        self.ptr = <double *>malloc(size)
+        if self.ptr is NULL:
             raise MemoryError("malloc failed")
-        self.guard = 3.14159
-        self._rawptr[0] = self.guard
-        self._rawptr[len + 1] = self.guard
-        self.ptr = self._rawptr + 1
+        global memoryUsage
+        memoryUsage = memoryUsage + size
+        if init:
+            for i from 0 <= i < self.len:
+                self.ptr[i] = init[i]
         return
 
     def __dealloc__(self):
-        if (self._rawptr[0] != self.guard or
-            self._rawptr[self.len + 1] != self.guard):
-            print "memory corrupted"
-        free(self._rawptr)
-        self._rawptr = NULL
+        free(self.ptr)
         self.ptr = NULL
+        global memoryUsage
+        memoryUsage = memoryUsage - (sizeof(double) * self.len)
         return
 
+    cdef asList(self):
+        return listFromDoubleArray(self.ptr, self.len)
 
+
+# utilities
+
+
+cdef setIntArrayFromList(int *array, int dim, aList):
+    assert(len(aList) == dim)
+    for i from 0 <= i < dim:
+        array[i] = aList[i]
+    return
+
+
+cdef listFromIntArray(int *array, int dim):
+    aList = []
+    for i from 0 <= i < dim:
+        aList.append(array[i])
+    return aList
+
+
+cdef setDoubleArrayFromList(double *array, int dim, aList):
+    assert(len(aList) == dim)
+    for i from 0 <= i < dim:
+        array[i] = aList[i]
+    return
+
+
+cdef listFromDoubleArray(double *array, int dim):
+    aList = []
+    for i from 0 <= i < dim:
+        aList.append(array[i])
+    return aList
+
+
 # end of file



More information about the cig-commits mailing list