JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for OX-USERS Archives


OX-USERS Archives

OX-USERS Archives


OX-USERS@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Monospaced Font

LISTSERV Archives

LISTSERV Archives

OX-USERS Home

OX-USERS Home

OX-USERS  July 2018

OX-USERS July 2018

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

Re: Cython-Ox Possible Memory Leak

From:

Jurgen Doornik <[log in to unmask]>

Reply-To:

Jurgen Doornik <[log in to unmask]>

Date:

Tue, 3 Jul 2018 17:51:45 +0100

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (292 lines)

Dear Alex,

I only had a cursory look, but could the problem be in your set_matrix?

OxValSetMat takes a copy of the matrix, but I don't see a free in set_matrix.

Jurgen

oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
oxox 20th OxMetrics user conference 10-11 Sept 2018
oxox Centre for Econometric Analysis, Cass Business School
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox
Dr Jurgen A Doornik
James Martin Fellow, Institute for New Economic Thinking
at the Oxford Martin School, University of Oxford
http://www.doornik.com
oxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxox

On 2018-07-03 11:38, Alex de Geus wrote:
> Hello,
>
> I am experiencing some large memory consumptions in an application using Cython
> to communicate with Ox.
>
> I am using the Ox Professional version 7.10 with the Development Kit with this
> manual as reference https://www.doornik.com/ox/OxDeveloper.pdf.
>
> Below are some code snippets with a simplified unit test that hands 2 large
> matrices to ox and back to python again.
>
> I have tried to clean up all the pointer allocations to Ox very carefully,
> however this does not seem to work correctly as memory keeps accumulating for
> every Ox call.
>
> I am pretty sure I am not freeing the memory allocated correctly for both the
> input and output arguments, however I have been unsuccessful in finding a solution.
>
> I have tried the void OxFreeByValue(OxVALUE*) function for the output arguments,
> but it does not seem to help, in some cases it even increases memory consumption.
>
> I have tried the void MatFreeBlock(MATRIX) function for the input and output
> arguments of type matrix, but this always seems to crash.
>
> Any idea on how to fix this or how to debug this code more effectively?
>
> Best,
>
> Alex
>
> Memory consumption (using the memory_profiler package from python):
>
> Line #    Mem usage    Increment   Line Contents
>
> ================================================
>
>     31     25.1 MiB     25.1 MiB    @profile
>
>     32                                         def test_getterssetters(self):
>
>     33
>
>     34     27.1 MiB      2.1 MiB                 with
> oxcy.create_ox_environment_from_docstring(self.docstring) as OxEnv:
>
>     35
>
>     36    790.1 MiB    762.9 MiB                           mat1_in =
> np.ones((10000, 10000))
>
>     37   1553.0 MiB    762.9 MiB                          mat2_in =
> np.ones((10000, 10000))
>
>     38
>
>     39   7096.0 MiB   5543.0 MiB                         mat1_out, mat2_out =
> OxEnv.TestGettersSetters(mat1_in, mat2_in)
>
>     40
>
>     41   7096.1 MiB      0.1 MiB
> np.testing.assert_allclose(mat1_in, mat1_out)
>
>     42   7095.4 MiB     -0.7 MiB
> np.testing.assert_allclose(mat2_in, mat2_out)
>
>     43
>
>     44   4043.7 MiB  -3051.8 MiB              del mat1_in, mat2_in, mat1_out,
> mat2_out
>
>     45
>
>     46   4044.4 MiB      0.8 MiB                with
> oxcy.create_ox_environment_from_docstring(self.docstring) as OxEnv:
>
>     47
>
>     48   4807.3 MiB    762.9 MiB                          mat1_in =
> np.ones((10000, 10000))
>
>     49   5570.3 MiB    762.9 MiB                          mat2_in =
> np.ones((10000, 10000))
>
>     50
>
>     51   8024.9 MiB   2454.6 MiB                          mat1_out, mat2_out =
> OxEnv.TestGettersSetters(mat1_in, mat2_in)
>
>     52
>
>     53   8079.7 MiB     54.8 MiB
> np.testing.assert_allclose(mat1_in, mat1_out)
>
>     54   8079.2 MiB     -0.5 MiB
> np.testing.assert_allclose(mat2_in, mat2_out)
>
>     55
>
>     56   5027.4 MiB  -3051.8 MiB              del mat1_in, mat2_in, mat1_out,
> mat2_out
>
> Python snippet:
>
> classTest_oxcy(unittest.TestCase):
>
> defsetUp(self):
>
> self.docstring = \
>
> """
>
> #include <oxstd.h>
>
> #include <packages/ssfpack/ssfpack_ex.h>
>
> TestGettersSetters(const mat1, const mat2);
>
> TestGettersSetters(const mat1, const mat2)
>
> {
>
>     decl output = new array[2];
>
>     output[0] = mat1;
>
>     output[1] = mat2;
>
>     return output;
>
> }
>
> """
>
> deftest_getterssetters(self):
>
> withoxcy.create_ox_environment_from_docstring(self.docstring) asOxEnv:
>
>             mat1_in = np.ones((10000, 10000))
>
>             mat2_in = np.ones((10000, 10000))
>
>             mat1_out, mat2_out = OxEnv.TestGettersSetters(mat1_in, mat2_in)
>
>             np.testing.assert_allclose(mat1_in, mat1_out)
>
>             np.testing.assert_allclose(mat2_in, mat2_out)
>
> delmat1_in, mat2_in, mat1_out, mat2_out
>
> withoxcy.create_ox_environment_from_docstring(self.docstring) asOxEnv:
>
>             mat1_in = np.ones((10000, 10000))
>
>             mat2_in = np.ones((10000, 10000))
>
>             mat1_out, mat2_out = OxEnv.TestGettersSetters(mat1_in, mat2_in)
>
>             np.testing.assert_allclose(mat1_in, mat1_out)
>
>             np.testing.assert_allclose(mat2_in, mat2_out)
>
> delmat1_in, mat2_in, mat1_out, mat2_out
>
> deftearDown(self):
>
> pass
>
> if__name__== "__main__":
>
>     unittest.main()
>
> Cython snippets:
>
> cdef classOxEnv:
>
> """ ox environment wrapper """
>
>     ...
>
> def__enter__(self):
>
> """ load ox source file into ox compiler """
>
> # TODO: check return value for response?
>
>         cox.OxMainCmd('-r- {}'.format(self.src))
>
> returnself
>
> def__exit__(self, exc_type, exc_message, exc_traceback):
>
> """ exit ox runtime environment """
>
>         ...
>
>         cox.OxRunExit()
>
>         cox.OxMainExit()
>
>     ...
>
> cdefcall_ox_func(strf_name):
>
> """ call ox static function """
>
> deffunc(*args):
>
> try:
>
>             f_ptr = cox.OxStoreCreate(1)
>
>             cox.OxValSetString(f_ptr, f_name)
>
>             in_ptr = cox.OxStoreCreate(len(args))
>
> fori in range(len(args)):
>
>                 set_ox_var(cox.OxValGetVal(in_ptr, i), args[i])
>
>             out_ptr = cox.OxStoreCreate(1)
>
> ifcox.FOxCallBack(f_ptr, out_ptr, in_ptr, len(args)) != 1:
>
> raiseException("Failed to call function!", f_name)
>
>             val = get_ox_var(out_ptr)
>
> finally:
>
>             cox.OxStoreDelete(f_ptr, 1)
>
> cox.OxStoreDelete(in_ptr, len(args))
>
> cox.OxStoreDelete(out_ptr, 1)
>
> returnval
>
> returnfunc
>
> # called by set_ox_var for matrix types
>
> cdefset_matrix(cox.OxVALUE *ptr, np.ndarray[np.double_t, ndim=2] m):
>
> """ write matrix to ptr """
>
>     cR, cC = m.shape[0], m.shape[1]
>
> matrix = cox.MatAllocBlock(cR, cC)
>
> fori in range(cR):
>
> forj in range(cC):
>
>             cox.MatSetAt(matrix, m[i, j], i, j)
>
>     cox.OxValSetMat(ptr, matrix, cR, cC)
>
>
>
> Please consider the environment before printing this document
>
> --------------------------------------------------------------------------------
>
> To unsubscribe from the OX-USERS list, click the following link:
> https://www.jiscmail.ac.uk/cgi-bin/webadmin?SUBED1=OX-USERS&A=1
>

########################################################################

To unsubscribe from the OX-USERS list, click the following link:
https://www.jiscmail.ac.uk/cgi-bin/webadmin?SUBED1=OX-USERS&A=1

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

April 2024
March 2024
February 2024
January 2024
November 2023
October 2023
September 2023
June 2023
May 2023
March 2023
February 2023
January 2023
May 2022
March 2022
January 2022
December 2021
July 2021
February 2021
November 2020
October 2020
September 2020
May 2020
April 2020
March 2020
January 2020
November 2019
October 2019
September 2019
August 2019
May 2019
February 2019
November 2018
October 2018
August 2018
July 2018
May 2018
April 2018
March 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
February 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
April 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
January 2002
December 2001
November 2001
October 2001
September 2001
August 2001
July 2001
June 2001
May 2001
April 2001
March 2001
February 2001
January 2001
December 2000
November 2000
October 2000
September 2000
August 2000
July 2000
June 2000
May 2000
April 2000
March 2000
February 2000
January 2000
December 1999
November 1999
October 1999
September 1999
August 1999
July 1999
June 1999
May 1999
April 1999
March 1999
February 1999
January 1999
December 1998
November 1998
October 1998
September 1998


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager