DeinoMPI Forum

Forum dedicated to discussing DeinoMPI
Welcome to DeinoMPI Forum Sign in | Join | Help
in Search

Writing C# Programs with DeinoMPI

Last post 02-06-2008, 8:15 PM by bigkahuna. 8 replies.
Sort Posts: Previous Next
  •  10-17-2007, 10:40 PM 180

    Writing C# Programs with DeinoMPI

    Hello,

    How can I compile a C# project using DeinoMPI?

    Thanks,
    Meni.

  •  10-18-2007, 1:30 AM 181 in reply to 180

    Re: Writing C# Programs with DeinoMPI

    DeinoMPI does not have a C# interface.  So if you want to use C# you will have to pin your buffers and use PInvoke to call the MPI functions.  Or you could write a managed C++ class that copies between the managed and unmanaged buffers and calls the MPI functions.  Or maybe you could be creative and come up with another solution but there isn't a pre-packaged solution from DeinoMPI.
  •  10-18-2007, 11:19 PM 183 in reply to 181

    Re: Writing C# Programs with DeinoMPI

    Hello!

    I decided to write a managed c++ class to wrap MPI function.
    I stumble into this weird Access violation when wrote this code:

    //void Init(int argc, char **argv)
    void
    Init()
    {
       array<String^>^ argv = System::Environment::GetCommandLineArgs
    ();
       int argc = argv->Length
    ;
       char ** pArgv = new char*[argc
    ];
       for (int i=0;i<argc;i
    ++)
       {
          std::string str = ToCppString(argv[i
    ]);
          int len = str.size
    ();
          pArgv[i] = new char[ len
    + 1];
          strcpy(pArgv[i], str.c_str
    ());
          std::cout << pArgv[i] << endl
    ;
       }
       MPI::Init(argc, pArgv
    ); // ACCESS VIOLATION exception is here!!!
       for (int i=0;i<argc;i
    ++)
          delete []pArgv[i
    ];
       delete pArgv
    ;
    }

    I had to do it that way because the C# program has no agrc&argv couple.
    Here is my client C# code:

    using MPI_for_cs;

    namespace Test_CS_MPI
    {
       class
    Program
      
    {
          static void Main(string[] args
    )
          {
             CS_MPI c1 = new CS_MPI
    ();
             c1.Init
    ();
             String name = c1.Get_processor_name
    ();
             Console.WriteLine(name
    );
          }
       }
    }

    I had to be sure what the cause of the crash, so I wrote another managed C++ client that uses the same class, but this time I pass it the original argc & agrv and the Init function implementation was simple: just call MPI::Init(argc, argv); the same parameters it gets it passes. It worked.
    Code:

    int main(int argc, char **argv)
    {
       Console::WriteLine(L"Hello World");
      
    Console::WriteLine(argc);
       CS_MPI mpi;
      
    mpi.Init(argc, argv);
      
    return 0;
    }

    What is wrong in the code I wrote by "manufacturing" my own argc & argv?
    If you want the projects themselves I'll send it by email.

    Thanks!

  •  10-19-2007, 5:28 PM 184 in reply to 183

    Re: Writing C# Programs with DeinoMPI

    All of your examples should have crashed with access violations because you are not passing the correct parameters to MPI_Init.  Your code should be: MPI_Init(&argc, &argv);

    MPI_Init takes the references to argc and argv.

    I suggest you pass NULL to MPI_Init and not bother with the command line.  This will work: MPI_Init(NULL, NULL);

  •  10-20-2007, 12:52 PM 187 in reply to 184

    Re: Writing C# Programs with DeinoMPI

    First of all, I tried to pass NULL, NULL - to lead to crash.
    What you said about passing &argc and &argv is not correct. If you were correct - the prototype of MPI::Init should look like this:

    Init(int *argc, char ***args)

    But it looks like this:

    Init(int &argc, char **& args)

    Which means that you pass it by reference, and not passing the pointer-of (with ampersand).

    Any ideas what causes the crash?

    I found a workaround, to declare a constant size array of char*:

    char * pArgv[100];
    for (int i=0;i<argc && i<100;i
    ++)
    {
       std::string str = ToCppString(arrArgv[i
    ]);
       int len = str.size
    ();
       pArgv[i] = new char[ len
    + 1];
       strcpy_s(pArgv[i], len+1, str.c_str
    ());
    }

    This works, again, I don't understand why. Maybe you can explain...\

    Thanks!

  •  10-20-2007, 6:05 PM 188 in reply to 187

    Re: Writing C# Programs with DeinoMPI

    I apologize, I read your post too quickly.  You are using the C++ interface and I suggested the C interface MPI_Init(NULL, NULL);  You can use MPI::Init(); with no parameters to ignore the command line.

    When creating a temporary argc and argv you need to realize that C# does not pass the path to the executable as the first argument.  So you will need to add an extra string to the array.

    I'm not able to tell why your code crashes with just the code snipets you've provided.

  •  10-22-2007, 3:53 PM 191 in reply to 188

    Re: Writing C# Programs with DeinoMPI

    Thanks.

    I done that and it's working... I missed that there is an overloaded function without parameters.

    BTW, why the Init function need the arguments of the exe?

  •  10-24-2007, 12:23 AM 195 in reply to 191

    Re: Writing C# Programs with DeinoMPI

    DeinoMPI does not use the command line.  But MPI implementations are allowed to pass implementation specific optional options on the command line.
  •  02-06-2008, 8:15 PM 223 in reply to 195

    Re: Writing C# Programs with DeinoMPI

    If you're looking for a fully managed implementation in C#, you should give purempi.net a try.
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems