| Thread overview | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 25, 2011 Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Attachments: | Ok, so I finally got mpi.h to compile and link with D. A few problems are left to fix, like the fact that I didn't realize that OpenMPI does not care about the types, unless you are building it... Anyways applied a temp fix and got the first program running! young@Dan-Laptop:~/bin/DlanguageBindings/OpenMPI/examples$ mpirun hello_d Hello, world, I am 0 of 1 Not great, but its a start... One thing that has been buggin me.. I don't fully understand how C handles the arguments given to the program, but I do know that MPI handles them directly. int MPI_Init(int *argc, const char ***argv); Is there an easy way to turn D style (string[] args) into C style? PS- MPI has it's own wrappers for the compilation of programs, mpicc. would it be within the scope of Deimos to provide a similar mpidc wrapper? | |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jude Young | Jude Young: > Is there an easy way to turn D style (string[] args) into C style? Maybe something like this (not tested)? const int argc = args.length; const char** argv = array(map!toStringz(args)).ptr; Bye, bearophile | |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jude Young | On 2011-11-25 09:13, Jude Young wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Ok, so I finally got mpi.h to compile and link with D. > > A few problems are left to fix, like the fact that I didn't realize > that OpenMPI does not care about the types, unless you are building it... > > Anyways applied a temp fix and got the first program running! > young@Dan-Laptop:~/bin/DlanguageBindings/OpenMPI/examples$ mpirun hello_d > Hello, world, I am 0 of 1 > > Not great, but its a start... > One thing that has been buggin me.. > I don't fully understand how C handles the arguments given to the > program, but I do know that MPI handles them directly. > int MPI_Init(int *argc, const char ***argv); > > > Is there an easy way to turn D style (string[] args) into C style? If you just want to forward the arguments your application received you can use _NSGetEnviron() function on Mac OS X and "environ" global variable on other Posix systems. There are probably similar function/variable available on Windows. -- /Jacob Carlborg | |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Taken with modifications from std.process.execv:
import core.stdc.stdlib;
import std.string;
import std.stdio;
import std.conv;
const(char)** argsToC(in string[] argv)
{
auto _argv = cast(const(char)**)malloc((char*).sizeof * (1 + argv.length));
toAStringz(argv, _argv);
return _argv;
}
private void toAStringz(in string[] a, const(char)** az)
{
foreach(string s; a)
{
*az++ = toStringz(s);
}
*az = null;
}
void main()
{
string[] args = ["foo", "bla"];
auto _argv = argsToC(args);
assert(to!string(_argv[0]) == "foo");
assert(to!string(_argv[1]) == "bla");
}
Take a look at the various execv/execvp in std.process, but be careful since most of them use alloca to allocate on the stack.
| ||||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Also, execve/execvpe passes the environment as well. It should be cross-platform if I'm not mistaken. | ||||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Fri 25 Nov 2011 03:12:34 AM CST, bearophile wrote: > Jude Young: > >> Is there an easy way to turn D style (string[] args) into C style? > > Maybe something like this (not tested)? > const int argc = args.length; > const char** argv = array(map!toStringz(args)).ptr; > > Bye, > bearophile > ...where is template map? nvm, found it. algorithm.. works, with minor alteration. int argc = args.length; const char** argv = array(map!toStringz(args)).ptr; Thanks! did I highjack an existing thread, or is my program just being stupid? | |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Attachments: | also more success!! //This wouldn't work before... - ------------------------------ time (mpirun -np 5 -v hello_d) Hello, world, I am 1 of 5 Hello, world, I am 4 of 5 Hello, world, I am 2 of 5 Hello, world, I am 5 of 5 Hello, world, I am 3 of 5 real 0m1.102s user 0m0.112s sys 0m0.096s - ---------------- Not real sure about why the spacing is like that... Another concern is about how mpirun is passing the arguments to the program... If THAT needs to be patched in the source that'll be a bitch. Another concern is about how long it takes to start... a whole second for 5 threads? 1 thread takes about .050... Is this normal for MPI? I guess I should probably check timings for other languages. Ok, now I'm looking for people who have any experience with MPI. how many of the files will need to be ported? None of the examples that I see use anything more than mpi.h. Are any of the other header files necessary? | |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jude Young | On 11/25/11 2:13 AM, Jude Young wrote:
> Is there an easy way to turn D style (string[] args) into C style?
Hm, I expected this would work:
extern(C) int __argc;
extern(C) char** __argv;
The symbols do exist at least on OSX (no linker error) but they are both zero, so apparently druntime doesn't initialize them.
Andrei
| |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu Attachments: | On 11/25/2011 06:27 AM, Andrei Alexandrescu wrote:
> On 11/25/11 2:13 AM, Jude Young wrote:
>> Is there an easy way to turn D style (string[] args) into C
>> style?
>
> Hm, I expected this would work:
>
> extern(C) int __argc; extern(C) char** __argv;
>
> The symbols do exist at least on OSX (no linker error) but they are both zero, so apparently druntime doesn't initialize them.
>
>
> Andrei
>
>
Hmmm. that's something I never expected.
It would be nice if that was always exposed. (and initialized)
More progress with Open MPI! from what I know now, I should have it working in less than a week. barring I don't learn that I did everything wrong.
| |||
November 25, 2011 Re: Open MPI with D | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 11/25/2011 02:27 PM, Andrei Alexandrescu wrote:
> On 11/25/11 2:13 AM, Jude Young wrote:
>> Is there an easy way to turn D style (string[] args) into C style?
>
> Hm, I expected this would work:
>
> extern(C) int __argc;
> extern(C) char** __argv;
>
> The symbols do exist at least on OSX (no linker error) but they are both
> zero, so apparently druntime doesn't initialize them.
>
>
> Andrei
>
If you want a reference to an externally defined variable, you need another "extern" and (if the variable is non-TLS) - __gshared:
__gshared extern extern(C) int __argc;
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply