473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Code conversion: C++ to C#

1
I need some help please.

I have to convert this code from c++ to c#.but i don't know how can i do it.and i have to do it ASAP.

can you convert it for me please?

thanks..im waiting you.


the code :

Expand|Select|Wrap|Line Numbers
  1. #include "mpi.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6.  
  7. int rank, size;
  8.  
  9. #define MAX_ALLOC_SIZE 1000000000
  10.  
  11. int isValidAlloc(int alloc){
  12.     return (alloc>0&&alloc<MAX_ALLOC_SIZE);
  13. }
  14.  
  15. void swap(int* data, int i, int j){
  16.     int t;
  17.     t = data[i];
  18.     data[i] = data[j];
  19.     data[j] = t;
  20. }
  21.  
  22. //  Q-sort algorithm
  23. void myqsort(int* data, int left, int right){
  24.     int i,last;
  25.     if(left>=right||left<0||right<0)
  26.         return;
  27.     //swap(data,left,(left+right)/2);
  28.     last = left;
  29.     for(i=left+1;i<=right;i++)
  30.         if(data[i]<data[left])
  31.             swap(data,++last,i);
  32.     swap(data,left,last);
  33.     myqsort(data,left,last-1);
  34.     myqsort(data,last+1,right);
  35. }
  36.  
  37. void qSort(int* buf , int bufSize){
  38.     myqsort(buf, 0, bufSize-1);
  39. }
  40.  
  41. //  Bubble sort algorithm
  42. void bubbleSort(int* buf, int bufSize){
  43.     for(int i=0;i<bufSize;i++)
  44.         for(int j=0;j<bufSize-i-1;j++){
  45.             if(buf[j]>buf[j+1])
  46.                 swap(buf, j, j+1);
  47.         }
  48. }
  49.  
  50. //  Merging sort algorithm
  51. void merge(int* input, int* output, int bufSize, int totalSize){
  52.     int* track=(int*)calloc(size, sizeof(int));
  53.     if(!track)
  54.         MPI_Abort(MPI_COMM_WORLD, -1);
  55.     for(int i=0;i<totalSize;i++){
  56.         int min=INT_MAX;
  57.         int minsite=-1;
  58.         for(int j=0;j<size;j++){
  59.             if(track[j]<bufSize&&*(input+bufSize*j+track[j])<=min){
  60.                 min=*(input+bufSize*j+track[j]);
  61.                 minsite=j;
  62.             }
  63.         }
  64.         output[i]=min;
  65.         track[minsite]++;
  66.     }
  67.     if(track)
  68.         free(track);
  69. }
  70.  
  71. //  Reads the sequence data and scatters to all the processes.
  72. void init_root(char* input_fn, int** smallBuffer, int* bufSize, int* cnt){
  73.     FILE* fp=fopen(input_fn, "r");
  74.  
  75.     if(!fp){
  76.         printf("Can't find the file: %s\n.", input_fn);
  77.         fflush(stdout);
  78.         MPI_Abort(MPI_COMM_WORLD, -1);
  79.     }
  80.  
  81.     fscanf_s(fp, "%d", cnt);
  82.  
  83.     *bufSize=*cnt/size+1;
  84.  
  85.     int toAlloc=sizeof(int)**bufSize*size;
  86.     if(!isValidAlloc(toAlloc))
  87.         MPI_Abort(MPI_COMM_WORLD, -1);
  88.     int* bigBuffer=(int*)malloc(toAlloc);
  89.     if(!bigBuffer)
  90.         MPI_Abort(MPI_COMM_WORLD, -1);
  91.  
  92.     for(int i=0;i<*bufSize*size;i++)
  93.         *(bigBuffer+i)=INT_MAX;
  94.  
  95.     toAlloc=sizeof(int)**bufSize;
  96.     if(!isValidAlloc(toAlloc))
  97.         MPI_Abort(MPI_COMM_WORLD, -1);
  98.     *smallBuffer=(int*)malloc(toAlloc);
  99.     if(!smallBuffer)
  100.         MPI_Abort(MPI_COMM_WORLD, -1);
  101.  
  102.     int proc=0;
  103.     int iter=0;
  104.     for(int i=0;i<*cnt;i++){
  105.         fscanf_s(fp, "%d", bigBuffer+proc**bufSize+iter);
  106.         if(++proc==size){
  107.             proc=0;
  108.             iter++;
  109.         }
  110.     }
  111.  
  112.     if(fp)
  113.         fclose(fp);
  114.  
  115.     MPI_Bcast(bufSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
  116.     MPI_Scatter(bigBuffer, *bufSize, MPI_INT, *smallBuffer, *bufSize, MPI_INT, 0, MPI_COMM_WORLD);
  117.     if(bigBuffer)
  118.         free(bigBuffer);
  119.  
  120. }
  121.  
  122. //  Gets the sequence data scattered from the root process.
  123. void init_worker(int** smallBuffer, int* bufSize){
  124.     int* bigBuffer=NULL;
  125.     MPI_Bcast(bufSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
  126.  
  127.     int toAlloc=sizeof(int)**bufSize;
  128.     if(!isValidAlloc(toAlloc))
  129.         MPI_Abort(MPI_COMM_WORLD, -1);
  130.     *smallBuffer=(int*)malloc(toAlloc);
  131.     if(!smallBuffer)
  132.         MPI_Abort(MPI_COMM_WORLD, -1);
  133.  
  134.     MPI_Scatter(bigBuffer, *bufSize, MPI_INT, *smallBuffer, *bufSize, MPI_INT, 0, MPI_COMM_WORLD);
  135. }
  136.  
  137. void main(int argc, char* argv[]){
  138.     MPI_Init(&argc, 0);
  139.     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  140.     MPI_Comm_size(
  141.         MPI_COMM_WORLD,
  142.         &size
  143.         );
  144.  
  145.     if(argc!=3){
  146.         if(rank==0){
  147.             printf("Usage: mpiexec -np <nProcs> sort.exe <input_file> <output_file>\n");
  148.             fflush(stdout);
  149.         }
  150.         MPI_Finalize();
  151.         return;
  152.     }
  153.  
  154.     char* input_fn=argv[1];
  155.     char* output_fn=argv[2];
  156.  
  157.     int* inBuf=NULL;
  158.     int bufSize=0;
  159.     int totalSize=0;
  160.  
  161.     if(rank==0)
  162.         init_root(input_fn, &inBuf, &bufSize, &totalSize);
  163.     else
  164.         init_worker(&inBuf, &bufSize);
  165.  
  166.     int *tmpBuf,*finalBuf;
  167.     tmpBuf=finalBuf=NULL;
  168.     if(rank==0)
  169.     {
  170.         int toAlloc=sizeof(int)*bufSize*size;
  171.         if(!isValidAlloc(toAlloc))
  172.             MPI_Abort(MPI_COMM_WORLD, -1);
  173.         tmpBuf=(int*)malloc(toAlloc);
  174.         if(!tmpBuf)
  175.             MPI_Abort(MPI_COMM_WORLD, -1);
  176.  
  177.         toAlloc=sizeof(int)*totalSize;
  178.         if(!isValidAlloc(toAlloc))
  179.             MPI_Abort(MPI_COMM_WORLD, -1);
  180.         finalBuf=(int*)malloc(toAlloc);
  181.         if(!finalBuf)
  182.             MPI_Abort(MPI_COMM_WORLD, -1);
  183.  
  184.     }
  185.  
  186.     //  Chooses a sorting algorithm for each process respectively.
  187.     void (*mysort)(int*, int)=(rank%2?qSort:bubbleSort);
  188.  
  189.     //(*mysort) (int*, int) = qSort ;
  190.     //  Sorting
  191.     mysort(inBuf, bufSize);
  192.  
  193.     //  Gathers all the sorted sections back to the root process.
  194.     MPI_Gather(inBuf, bufSize, MPI_INT, tmpBuf, bufSize, MPI_INT, 0, MPI_COMM_WORLD);
  195.  
  196.     if(rank==0)
  197.     {
  198.  
  199.         //  Merges all the well-sorted sequence sections together to the final result.
  200.         merge(tmpBuf, finalBuf, bufSize, totalSize);
  201.  
  202.         //  Outputs to file.
  203.         FILE* fp=fopen(output_fn, "w");
  204.         if(!fp)
  205.             MPI_Abort(MPI_COMM_WORLD, -1);
  206.         fprintf(fp, "%d\n", totalSize);
  207.         for(int i=0;i<totalSize;i++){
  208.             fprintf(fp, "%d ", *(finalBuf+i));
  209.         }
  210.         if(fp)
  211.             fclose(fp);
  212.     }
  213.  
  214.     if(inBuf)
  215.         free(inBuf);
  216.     MPI_Finalize();
  217. }
  218.  
Apr 16 '09 #1
3 2322
Bassem
344 100+
These may help you StreamReader, StreamWriter and Unsafe code.
Try and post if any problem.
Apr 16 '09 #2
PRR
750 Expert 512MB
C++ and C# are different languages... It wont be easy to convert c++ code to C# in one step... You can look Lutz Roeder's Reflector
Apr 16 '09 #3
tlhintoq
3,525 Expert 2GB
can you convert it for me please?
No. If you want to offer this as a paid job request there is a forum for that... but otherwise the people hear are volunteers who are happy to help you learn, but they won't do your work for you.
Apr 16 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Aakash Bordia | last post by:
Hello, Does anybody know what is the documented and known behavior of inserting/updating binary columns using host variables from a client to a server which have different code pages? Will any...
5
by: Darren Grant | last post by:
Hi there, I've attempted to implement an Angle class. An Angle is a subset of an integer, where the range is [0,360). All other operations should be permitted. The code works, I think......
13
by: Christopher Benson-Manica | last post by:
This is intended to be a simple version of the Unix "head" command, i.e. a utility that displays the first n lines of a file. Comments welcomed... #include <cstdlib> #include <iostream>...
4
by: yanyo | last post by:
hi, im trying to figure out whats the problem with this program i get a runtime error but i dont see where the problem is i tried changing declaration but nothing if somrbody can try this on their...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: nan | last post by:
Hi All, I am trying to connect the Database which is installed in AS400 using DB2 Client Version 8 in Windows box. First i created the Catalog, then when i selected the connection type...
7
by: dtecmeister | last post by:
Looking to see how many people could use this kind of tool. I've got several large databases I've developed in Access with MySQL as the back-end. I've started using Linux instead of windows and...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
2
by: Netwatcher | last post by:
Hello, i am new to c++ windows and DX programming, i encountered a code in the book stated in the title, which doesn't work for a reason, here is the code // Beginning Game Programming // Chapter...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.