Surprising that & would crash, though possible.
When you use &, the safe way is to apply memu to the value that you are
assigning to the name that is going to go into cd. So if you use
ret =. (sqrdll, 'sqr > i i *i &i') cd (#y);y;res
you should have written
res =. memu (#y)#4 NB. make sure it's integer
Likewise if you are going to write over y you could use & but you would need to
execute (y =. memu y). If you know that y is not going to be modified by the DLL
you don't need to modify y.
If the function-call letter doesn't match the actual type of the data, the data
is copied. Best if you keep the sizes of integers the same. On J64, J's
integers are long long.
If the crashing code is short feel free to post it here, or send it to me
privately, for debugging help.
Henry Rich
On 11/19/2018 12:17 AM, Ben Gorte wrote:
When trying & , I apparently do not know what I am doing, because it
immediately crashes my session.
However, I do feel meanwhile that I get an increasing number of copies of
my (large) dataset in memory, so perhaps I should find out what I should be
doing. As I understand, & can help to avoid copying the data.
The basic situation is that I pass three arrays to my DLL function:
- a one-dimensional array with N integers,
- another one with N bytes (literals),
- and a 2D one with N x 2 integers.
N is up to of few hundred millions (but larger would be even better). The
first array is modified by the DLL; the others are 'read-only'.
Any suggestions are appreciated. In my function, the integers can be int or
long - what would be preferred? If they are the same size in J and C, does
it avoid (or reduce) copying?
Thanks again,
Ben
On Mon, Nov 19, 2018 at 1:58 PM bill lam <[email protected]> wrote:
"The memory is unaliased." refers to the memory as seen by
the callee of cd. Any changes made by callee won't be written
back to the original memory. That's why the value of names
remained unchanged after cd call. Unless you had taken
the address of name and passed the address to cd.
The release note also mentioned & but use it only if you
know what you are doing.
Mon, 19 Nov 2018, Ben Gorte написал(а):
Dear Henry, Bill, Linda,
Thanks for your responses, I am getting the point/ers.
Perhaps in
https://code.jsoftware.com/wiki/Guides/DLLs/Calling_DLLs#J807_Incompatibilities
, the phrase: "The DLL can read or write this memory." is a bit
confusing,
although it also says "The memory is unaliased." That means "copied if
required", and I thought it wouldn't be...
Anyway, all is well again,
Thanks,
Ben
On Mon, Nov 19, 2018 at 1:15 AM Henry Rich <[email protected]> wrote:
What Bill is saying is that starting in 807, cd makes an copy of any
argument to cd that might be shared by another noun. So, the value of
'res' is put into a temporary block and passed to the DLL. The
temporary block is put into the boxed result of cd. You need to look
in
that boxed result, not in the name res, which has not been modified.
This change was made in 807 because that release has a much more
aggressive policy of sharing nouns and parts of nouns without making
separate copies; for example
({. name)
actually gives you a pointer to the first item of name, without
recopying it, and
(i. 3)
is liable to give you a pointer to unchangeable internal data. With all
these pointers flying around we can no longer trust the user to know
when it is safe to pass a pointer into a DLL that might modify the
data.
Henry Rich
On 11/18/2018 7:55 AM, bill lam wrote:
j807 must get from result of cd, like this
0 3{ (sqrdll, 'sqr i i *i *i') cd (#y);y;res
On Sun, Nov 18, 2018, 2:22 PM Ben Gorte <[email protected] wrote:
Dear All,
Long ago I made quite a lot of c-functions that I am calling from J
using
the cd mechanism (load 'dll') in linux.
They stopped working in j64-807. I see in the release notes that
indeed
something has changed, but I cannot quite relate that to my case
(but
maybe
I should - I'm not sure I entirely understand the story).
In the simplest form it looks like this:
First the c-code, sqr.c :
int sqr (int n, int a[], int b[])
{
int i, sumsqr=0;
for (i=0; i<n; i++)
sumsqr += b[i] = a[i] * a[i];
return sumsqr;
}
which I compile with
gcc -O3 -shared -fPIC -o sql.so -Wl,--no-as-needed sqr.c
(omitting -O3 does not make a difference).
In J I go like:
load 'dll'
sqrdll =: jpath '~user/ext/sqr.so '
sqr =: 3 : 0
res =. (#y)#4 NB. make sure it's integer
ret =. (sqrdll, 'sqr > i i *i *i') cd (#y);y;res
ret, res
)
Up until j806 I would get:
sqr i.10
285 0 1 4 9 16 25 36 49 64 81
But in j807:
sqr i.10
285 4 4 4 4 4 4 4 4 4 4
So my DLL 'reads' an array alright, but seems to be not 'writing'
it.
For completeness:
JVERSION
Engine: j807/j64/linux
Release: commercial/2018-10-04T15:00:41
Library: 8.07.20
Qt IDE: 1.7.7/5.5.1
Platform: Linux 64
Installer: J807 install
InstallPath: /home/ben/j64-807
Contact: www.jsoftware.com
Perhaps I'm overlooking something trivial; any help is highly
appreciated!
Ben
----------------------------------------------------------------------
For information about J forums see
http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see
http://www.jsoftware.com/forums.htm
---
This email has been checked for viruses by AVG.
https://www.avg.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
gpg --keyserver subkeys.pgp.net --armor --export 4434BAB3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm