Csound Csound-dev Csound-tekno Search About

ctcsound table access details

Date2016-12-02 23:25
FromOeyvind Brandtsegg
Subjectctcsound table access details
I'm trying to optimize something, that involves table data processing
in both Csound and Python.
To be able to optimize efficiently, I'm trying to understand how much
data is copied and how frequently.

My understanding is that tableCopyOut (and tableCopyIn) copies the
whole table data when called,
but what about the ctcsound "table" method?

According to the docstring it returns a pointer to the table as an
ndarray. I'm a bit confused by the description, as the ndarray will
subsequently contain the ftable data, and it looks to me like it uses
csoundGetTable to copy the whole contents of the table to the ndarray.
Perhaps (likely) I misunderstand what is happening there? How does the
ctcsound "table" method work (i.e. what exactly happens when I call
it)?

best
Oeyvind

Date2016-12-03 09:51
FromFrancois PINOT
SubjectRe: ctcsound table access details
csoundGetTable returns a pointer to the data memory space of the ftable. The "table" method in ctcsound wraps this pointer in a numpy array. So there's no data copy, the numpy array and the csound ftable share the same memory.

Warning: once the numpy array is created, certain numpy vectorial operations may copy the array to a new one and in that case the new numpy array would not share anymore the same memory blocks than the csound ftable. As long as you read the data in the numpy array, or modify them one by one there's no risk. But for example imagine that you do something like this:

a = cs.table(n)
b = 'an numpy array with same shape and dims than a'
a = a + b

This could yield numpy to create a new array for a and then the data space of a is different from the data space of ftable # n in csound!

To avoid this, you should do in place vectorial operations like this:

a += b

This is explained here for example: http://ipython-books.github.io/featured-01/

François

2016-12-03 0:25 GMT+01:00 Oeyvind Brandtsegg <oyvind.brandtsegg@ntnu.no>:
I'm trying to optimize something, that involves table data processing
in both Csound and Python.
To be able to optimize efficiently, I'm trying to understand how much
data is copied and how frequently.

My understanding is that tableCopyOut (and tableCopyIn) copies the
whole table data when called,
but what about the ctcsound "table" method?

According to the docstring it returns a pointer to the table as an
ndarray. I'm a bit confused by the description, as the ndarray will
subsequently contain the ftable data, and it looks to me like it uses
csoundGetTable to copy the whole contents of the table to the ndarray.
Perhaps (likely) I misunderstand what is happening there? How does the
ctcsound "table" method work (i.e. what exactly happens when I call
it)?

best
Oeyvind

--

Oeyvind Brandtsegg
Professor of Music Technology
NTNU
7491 Trondheim
Norway
Cell: +47 92 203 205

http://www.partikkelaudio.com/
http://crossadaptive.hf.ntnu.no
http://gdsp.hf.ntnu.no/
http://soundcloud.com/brandtsegg
http://flyndresang.no/
http://soundcloud.com/t-emp

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here

Csound mailing list Csound@listserv.heanet.ie https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to https://github.com/csound/csound/issues Discussions of bugs and features can be posted here

Date2016-12-03 23:05
FromOeyvind Brandtsegg
SubjectRe: ctcsound table access details
That's pure magic. Thank you!

2016-12-03 1:51 GMT-08:00 Francois PINOT :
> csoundGetTable returns a pointer to the data memory space of the ftable. The
> "table" method in ctcsound wraps this pointer in a numpy array. So there's
> no data copy, the numpy array and the csound ftable share the same memory.
>
> Warning: once the numpy array is created, certain numpy vectorial operations
> may copy the array to a new one and in that case the new numpy array would
> not share anymore the same memory blocks than the csound ftable. As long as
> you read the data in the numpy array, or modify them one by one there's no
> risk. But for example imagine that you do something like this:
>
> a = cs.table(n)
> b = 'an numpy array with same shape and dims than a'
> a = a + b
>
> This could yield numpy to create a new array for a and then the data space
> of a is different from the data space of ftable # n in csound!
>
> To avoid this, you should do in place vectorial operations like this:
>
> a += b
>
> This is explained here for example:
> http://ipython-books.github.io/featured-01/
>
> François
>
> 2016-12-03 0:25 GMT+01:00 Oeyvind Brandtsegg :
>>
>> I'm trying to optimize something, that involves table data processing
>> in both Csound and Python.
>> To be able to optimize efficiently, I'm trying to understand how much
>> data is copied and how frequently.
>>
>> My understanding is that tableCopyOut (and tableCopyIn) copies the
>> whole table data when called,
>> but what about the ctcsound "table" method?
>>
>> According to the docstring it returns a pointer to the table as an
>> ndarray. I'm a bit confused by the description, as the ndarray will
>> subsequently contain the ftable data, and it looks to me like it uses
>> csoundGetTable to copy the whole contents of the table to the ndarray.
>> Perhaps (likely) I misunderstand what is happening there? How does the
>> ctcsound "table" method work (i.e. what exactly happens when I call
>> it)?
>>
>> best
>> Oeyvind
>>
>> --
>>
>> Oeyvind Brandtsegg
>> Professor of Music Technology
>> NTNU
>> 7491 Trondheim
>> Norway
>> Cell: +47 92 203 205
>>
>> http://www.partikkelaudio.com/
>> http://crossadaptive.hf.ntnu.no
>> http://gdsp.hf.ntnu.no/
>> http://soundcloud.com/brandtsegg
>> http://flyndresang.no/
>> http://soundcloud.com/t-emp
>>
>> Csound mailing list
>> Csound@listserv.heanet.ie
>> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
>> Send bugs reports to
>>         https://github.com/csound/csound/issues
>> Discussions of bugs and features can be posted here
>
>
> Csound mailing list Csound@listserv.heanet.ie
> https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND Send bugs reports to
> https://github.com/csound/csound/issues Discussions of bugs and features can
> be posted here



-- 

Oeyvind Brandtsegg
Professor of Music Technology
NTNU
7491 Trondheim
Norway
Cell: +47 92 203 205

http://www.partikkelaudio.com/
http://crossadaptive.hf.ntnu.no
http://gdsp.hf.ntnu.no/
http://soundcloud.com/brandtsegg
http://flyndresang.no/
http://soundcloud.com/t-emp

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here