I don't know how can i conver c++ code to c# code
can ypu help me?
code:
/*
2 * This is a MPI based sorting program.
3 * Copyright (c) Microsoft Corporation. All rights reserved.
4 */
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int rank, size;
#define MAX_ALLOC_SIZE 1000000000
int isValidAlloc(int alloc){
return (alloc>0&&alloc<MAX_ALLOC_SIZE);
}
void swap(int* data, int i, int j){
int t;
t = data[i];
data[i] = data[j];
data[j] = t;
}
// Q-sort algorithm
void myqsort(int* data, int left, int right){
int i,last;
if(left>=right||left<0||right<0)
return;
//swap(data,left,(left+right)/2);
last = left;
for(i=left+1;i<=right;i++)
if(data[i]<data[left])
swap(data,++last,i);
swap(data,left,last);
myqsort(data,left,last-1);
myqsort(data,last+1,right);
}
void qSort(int* buf , int bufSize){
myqsort(buf, 0, bufSize-1);
}
// Bubble sort algorithm
void bubbleSort(int* buf, int bufSize){
for(int i=0;i<bufSize;i++)
for(int j=0;j<bufSize-i-1;j++){
if(buf[j]>buf[j+1])
swap(buf, j, j+1);
}
}
// Merging sort algorithm
void merge(int* input, int* output, int bufSize, int totalSize){
int* track=(int*)calloc(size, sizeof(int));
if(!track)
MPI_Abort(MPI_COMM_WORLD, -1);
for(int i=0;i<totalSize;i++){
int min=INT_MAX;
int minsite=-1;
for(int j=0;j<size;j++){
if(track[j]<bufSize&&*(input+bufSize*j+track[j])<=min){
min=*(input+bufSize*j+track[j]);
minsite=j;
}
}
output[i]=min;
track[minsite]++;
}
if(track)
free(track);
}
// Reads the sequence data and scatters to all the processes.
void init_root(char* input_fn, int** smallBuffer, int* bufSize, int* cnt){
FILE* fp=fopen(input_fn, "r");
if(!fp){
printf("Can't find the file: %sn.", input_fn);
fflush(stdout);
MPI_Abort(MPI_COMM_WORLD, -1);
}
fscanf_s(fp, "%d", cnt);
*bufSize=*cnt/size+1;
int toAlloc=sizeof(int)**bufSize*size;
if(!isValidAlloc(toAlloc))
MPI_Abort(MPI_COMM_WORLD, -1);
int* bigBuffer=(int*)malloc(toAlloc);
if(!bigBuffer)
MPI_Abort(MPI_COMM_WORLD, -1);
for(int i=0;i<*bufSize*size;i++)
*(bigBuffer+i)=INT_MAX;
toAlloc=sizeof(int)**bufSize;
if(!isValidAlloc(toAlloc))
MPI_Abort(MPI_COMM_WORLD, -1);
*smallBuffer=(int*)malloc(toAlloc);
if(!smallBuffer)
MPI_Abort(MPI_COMM_WORLD, -1);
int proc=0;
int iter=0;
for(int i=0;i<*cnt;i++){
fscanf_s(fp, "%d", bigBuffer+proc**bufSize+iter);
if(++proc==size){
proc=0;
iter++;
}
}
if(fp)
fclose(fp);
MPI_Bcast(bufSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(bigBuffer, *bufSize, MPI_INT, *smallBuffer, *bufSize, MPI_INT, 0, MPI_COMM_WORLD);
if(bigBuffer)
free(bigBuffer);
}
// Gets the sequence data scattered from the root process.
void init_worker(int** smallBuffer, int* bufSize){
int* bigBuffer=NULL;
MPI_Bcast(bufSize, 1, MPI_INT, 0, MPI_COMM_WORLD);
int toAlloc=sizeof(int)**bufSize;
if(!isValidAlloc(toAlloc))
MPI_Abort(MPI_COMM_WORLD, -1);
*smallBuffer=(int*)malloc(toAlloc);
if(!smallBuffer)
MPI_Abort(MPI_COMM_WORLD, -1);
MPI_Scatter(bigBuffer, *bufSize, MPI_INT, *smallBuffer, *bufSize, MPI_INT, 0, MPI_COMM_WORLD);
}
void main(int argc, char* argv[]){
MPI_Init(&argc, 0);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(
MPI_COMM_WORLD,
&size
);
if(argc!=3){
if(rank==0){
printf("Usage: mpiexec -np <nProcs> sort.exe <input_file> <output_file>n");
fflush(stdout);
}
MPI_Finalize();
return;
}
char* input_fn=argv[1];
char* output_fn=argv[2];
int* inBuf=NULL;
int bufSize=0;
int totalSize=0;
if(rank==0)
init_root(input_fn, &inBuf, &bufSize, &totalSize);
else
init_worker(&inBuf, &bufSize);
int *tmpBuf,*finalBuf;
tmpBuf=finalBuf=NULL;
if(rank==0)
{
int toAlloc=sizeof(int)*bufSize*size;
if(!isValidAlloc(toAlloc))
MPI_Abort(MPI_COMM_WORLD, -1);
tmpBuf=(int*)malloc(toAlloc);
if(!tmpBuf)
MPI_Abort(MPI_COMM_WORLD, -1);
toAlloc=sizeof(int)*totalSize;
if(!isValidAlloc(toAlloc))
MPI_Abort(MPI_COMM_WORLD, -1);
finalBuf=(int*)malloc(toAlloc);
if(!finalBuf)
MPI_Abort(MPI_COMM_WORLD, -1);
}
// Chooses a sorting algorithm for each process respectively.
void (*mysort)(int*, int)=(rank%2?qSort:bubbleSort);
//(*mysort) (int*, int) = qSort ;
// Sorting
mysort(inBuf, bufSize);
// Gathers all the sorted sections back to the root process.
MPI_Gather(inBuf, bufSize, MPI_INT, tmpBuf, bufSize, MPI_INT, 0, MPI_COMM_WORLD);
if(rank==0)
{
// Merges all the well-sorted sequence sections together to the final result.
merge(tmpBuf, finalBuf, bufSize, totalSize);
// Outputs to file.
FILE* fp=fopen(output_fn, "w");
if(!fp)
MPI_Abort(MPI_COMM_WORLD, -1);
fprintf(fp, "%dn", totalSize);
for(int i=0;i<totalSize;i++){
fprintf(fp, "%d ", *(finalBuf+i));
}
if(fp)
fclose(fp);
}
if(inBuf)
free(inBuf);
MPI_Finalize();
}
thanks
Discuss This Question: 1 Reply