[m-users.] Error with static linking for the rmath library

Mark Clements mark.clements at ki.se
Mon Feb 13 20:51:58 AEDT 2023


Peter,

Thank you for considering this admittedly somewhat obscure error.

I have updated the gist to include a call to the library. The error is unchanged.

As to the why: the rmath library includes a wide range of non-uniform random numbers, however the provided uniform random number generator is of poor quality. Using static linking, it is possible to over-ride (or shadow) those two C functions to provide a user-defined uniform random number generator (see my initial post for a URL with details).

A further question is why one would want non-uniform random numbers in Mercury. I am gradually writing code for doing discrete event simulations in Mercury -- but this is, as much as anything, a distraction from grant writing:).

Sincerely, Mark.
________________________________
From: Peter Wang <novalazy at gmail.com>
Sent: 13 February 2023 01:14
To: Mark Clements <mark.clements at ki.se>
Cc: users <users at lists.mercurylang.org>
Subject: Re: [m-users.] Error with static linking for the rmath library

[You don't often get email from novalazy at gmail.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

On Sat, 11 Feb 2023 11:30:30 +0000 Mark Clements <mark.clements at ki.se> wrote:
> For the rmath library in Mercury (https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmclements%2Fmercury-rmath&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Y9HAeIxrHvPRDwwa%2BOB4YIfkLNSpFjgJb%2BzqYwf6qXs%3D&reserved=0), I am trying to use C code for the Mersenne Twister random number generator to replace the default random number generator. This involves replacing two C functions: double unif_rand(void) and void set_seed(unsigned int, unsigned int); for details, see https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Frstudio.github.io%2Fr-manuals%2Fr-admin%2FThe-standalone-Rmath-library.html&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=9p38pNc2FOO1IUTZMQJvdaxu7dHNKLRLicw1%2BdJrraw%3D&reserved=0.
>
> I have got this working -- _except_ if any of four functions (dwilcox, pwilcox, qwilcox, rwilcox) are included in the interface section (see the gist at https://eur01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgist.github.com%2Fmclements%2F68aa486f6ef4cfc9634076dad357e483&data=05%7C01%7Cmark.clements%40ki.se%7C89726bfe110a4f93222d08db0d5748ca%7Cbff7eef1cf4b4f32be3da1dda043c05d%7C0%7C0%7C638118440755417933%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=sK7vCrngePILn8a7lmnFilUpyZGCIv%2BUdb%2BpB1mR28M%3D&reserved=0). I then get a linking error (make minimal_fails):
>
> /usr/bin/ld: /usr/lib/libRmath.a(std_unif.o): in function `set_seed':
> (.text+0x0): multiple definition of `set_seed'; Mercury/os/minimal_fails.o:minimal_fails.c:(.text+0xb0): first defined here
> /usr/bin/ld: /usr/lib/libRmath.a(std_unif.o): in function `unif_rand':
> (.text+0x40): multiple definition of `unif_rand'; Mercury/os/minimal_fails.o:minimal_fails.c:(.text+0xc0): first defined here
>
> If I move those functions to the implementation section, then the linking is okay (make minimal_ok).
>
> Can anyone suggest why this fails and then works?

Your minimal_ok.m doesn't use dwilcox anywhere, so the code for it is
completely missing from the minimal_ok.c file. The linker probably has
no need to pull in librmath at all (or a specific .o file in the archive),
thus avoiding the multiple definition error.

Why are you defining set_seed and unif_rand anyway? Multiple definitions
of the same symbol name are not normally allowed.

Peter



N?r du skickar e-post till Karolinska Institutet (KI) inneb?r detta att KI kommer att behandla dina personuppgifter. H?r finns information om hur KI behandlar personuppgifter<https://ki.se/medarbetare/integritetsskyddspolicy>.


Sending email to Karolinska Institutet (KI) will result in KI processing your personal data. You can read more about KI's processing of personal data here<https://ki.se/en/staff/data-protection-policy>.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20230213/b316be6d/attachment.html>


More information about the users mailing list