codice:
#include <stdio.h>
#include <ctime>
#include <cstdlib>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <mpi.h>
#include <windows.h>
#include <stdlib.h>
#include <assert.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctime>
#include <set>
#include <cstdlib>
#include <time.h>
#include <algorithm>
#include <math.h>
#include <glut.h>
using namespace std;
const int RMAX = 10;
int globalSize=5;
int punti[5];
int iteratore=0;
int my_rankGlobale, sizeGlobale;
MPI_Comm comm;
void Distr_list(int local_A[], int local_n, int size,int rank,int global_B[],int d)
{
int c=0;
int f=local_n;
if(rank==(size-1))
{
c=(size-1)*local_n;
f=f+d;
}
else
c=rank*local_n;
int j=0;
for(int i=c;i<(f+c);i++)
{
local_A[j]=global_B[i];
j++;
}
}
static GLint wh=250,ww=250;
static GLfloat size = 3.0;
static GLfloat spin = 0.0;
HWND hwnd;
void drawLine(int p[])
{
printf("\n\n l'array ha valori:");
for(int i=0;i<globalSize;i++)
{
printf(" %d",p[i]);
}
printf("\n\n");
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(1.0,1.0,1.0,1.0);
//glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
int y=-125;
for(int i=0;i<globalSize;i++)
{
glPushMatrix();
y+=5;
glColor3f(1.0, 1.0, 1.0);
glLineWidth(15.0f);
glBegin(GL_LINES);
glColor3f(1.0,3.0,1.0);
glVertex3f(-168.0,y,0.0f);
glColor3f(2.0,3.0,2.0);
glVertex3f( p[i],y,0.0f);
glEnd();
// glLoadIdentity();
glPopMatrix();
//glFlush();
}
glutSwapBuffers();
}
void Print_listPoint(int local_A[], int local_n, int rank) {
int i;
printf("\n%d: ", rank);
for (i = 0; i < local_n; i++)
printf("%d ", local_A[i]);
printf("\n");
}
void OrdinaY(int array[],int size)
{
int c=40;
for(int i=0;i<size;i++)
{
array[i]=600-array[i];
c+=10;
}
}
void Print_global_list(int local_A[], int local_n, int my_rank, int p,
MPI_Comm comm)
{
int* A;
int i, n;
if (my_rank == 0) {
n = p*local_n;
A = (int*) malloc(n*sizeof(int));
MPI_Gather(local_A, local_n, MPI_INT, A, local_n, MPI_INT, 0,
comm);
printf("Global list:\n");
for (i = 0; i < n; i++)
printf("%d ", A[i]);
printf("\n\n");
//free(A);
} else {
MPI_Gather(local_A, local_n, MPI_INT, A, local_n, MPI_INT, 0,
comm);
}
}
int Compare(const void* a_p, const void* b_p) {
int a = *((int*)a_p);
int b = *((int*)b_p);
if (a < b)
return -1;
else if (a == b)
return 0;
else /* a > b */
return 1;
}
void Merge_split_low(int local_A[], int temp_B[], int temp_C[],
int local_n) {
int ai, bi, ci;
ai = 0;
bi = 0;
ci = 0;
while (ci < local_n) {
if (local_A[ai] <= temp_B[bi]) {
temp_C[ci] = local_A[ai];
ci++; ai++;
} else {
temp_C[ci] = temp_B[bi];
ci++; bi++;
}
}
memcpy(local_A, temp_C, local_n*sizeof(POINT));
}
void Merge_split_high(int local_A[], int temp_B[], int temp_C[],
int local_n) {
int ai, bi, ci;
ai = local_n-1;
bi = local_n-1;
ci = local_n-1;
while (ci >= 0) {
if (local_A[ai] >= temp_B[bi]) {
temp_C[ci] = local_A[ai];
ci--; ai--;
} else {
temp_C[ci] = temp_B[bi];
ci--; bi--;
}
}
memcpy(local_A, temp_C, local_n*sizeof(POINT));
}
void Odd_even_iter(int local_A[], int temp_B[], int temp_C[],
int local_n, int phase, int even_partner, int odd_partner,
int my_rank, int p, MPI_Comm comm) {
//
// printf("\n oven inizio \n");
// Print_global_list(local_A,local_n,my_rank,p,comm);
MPI_Status status;
if (phase % 2 == 0) {
if (even_partner >= 0)
{
MPI_Sendrecv(local_A, local_n, MPI_INT, even_partner, 0,
temp_B, local_n, MPI_INT, even_partner, 0, comm,
&status);
if (my_rank % 2 != 0)
{
Merge_split_high(local_A, temp_B, temp_C, local_n);
Print_global_list(local_A, local_n, my_rankGlobale, sizeGlobale, comm);
}
else
{
Merge_split_low(local_A, temp_B, temp_C, local_n);
Print_global_list(local_A, local_n, my_rankGlobale, sizeGlobale, comm);
}
}
} else { /* odd phase */
if (odd_partner >= 0) {
MPI_Sendrecv(local_A, local_n, MPI_INT, odd_partner, 0,
temp_B, local_n, MPI_INT, odd_partner, 0, comm,
&status);
if (my_rank % 2 != 0)
{
Merge_split_low(local_A, temp_B, temp_C, local_n);
Print_global_list(local_A, local_n, my_rankGlobale, sizeGlobale, comm);
}
else
{
Merge_split_high(local_A, temp_B, temp_C, local_n);
Print_global_list(local_A, local_n, my_rankGlobale, sizeGlobale, comm);
}
}
}
}
void Sort(int local_A[], int local_n, int my_rank,int p, MPI_Comm comm)
{
int phase;
int *temp_B, *temp_C;
int even_partner; /* phase is even or left-looking */
int odd_partner; /* phase is odd or right-looking */
/* Temporary storage used in merge-split */
temp_B = (int*) malloc((local_n)*sizeof(int));
temp_C = (int*) malloc((local_n)*sizeof(int));
/* Find partners: negative rank => do nothing during phase */
if (my_rank % 2 != 0) {
even_partner = my_rank - 1;
odd_partner = my_rank + 1;
if (odd_partner == p) odd_partner = -1; // Idle during odd phase
} else {
even_partner = my_rank + 1;
if (even_partner == p) even_partner = -1; // Idle during even phase
odd_partner = my_rank-1;
}
/* Sort local list using built-in quick sort */
qsort(local_A, local_n, sizeof(int), Compare);
printf("\n lista speriamo ordinaata bene\n");
Print_listPoint(local_A,local_n,my_rank);
for (phase = 0; phase < p; phase++)
{
Odd_even_iter(local_A, temp_B, temp_C, local_n, phase,
even_partner, odd_partner, my_rank, p, comm);
}
free(temp_B);
free(temp_C);
}
void f(int i)
{
int local_n;
int c=0;
int f=0;
int somma=0;
local_n=(globalSize)/sizeGlobale;
somma=local_n*sizeGlobale;
if(somma==(globalSize))
{
}
else
{
c=(globalSize)-somma;
if(c>sizeGlobale)
{
local_n+=1;
c=c-sizeGlobale;
}
if(my_rankGlobale==sizeGlobale-1)
f=c;
}
int local_A[local_n+f];
Print_listPoint(punti,globalSize,my_rankGlobale);
Distr_list(local_A,local_n,sizeGlobale,my_rankGlobale,punti,c);
Print_listPoint(local_A,local_n+f,my_rankGlobale);
Sort(local_A, local_n+f, my_rankGlobale, sizeGlobale, comm);
Print_global_list(local_A, local_n, my_rankGlobale, sizeGlobale, comm);
printf("\ndopo la global list\n");
for(int i=0;i<globalSize;i++)
printf("%d",local_A[i]);
printf("\n\n");
memcpy(punti, local_A, local_n*sizeof(int));
// OrdinaY(punti,globalSize);
drawLine(punti);
free(local_A);
}
void display(void)
{
int h;
for(int j=0;j<globalSize;j++)
{
h=(rand()%340)-120;
punti[j]=h;
}
drawLine(punti);
int q=glutCreateMenu(f);
glutAddMenuEntry("ordina i punti",1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
// glShadeModel (GL_FLAT);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat fAspect;
fAspect = (GLfloat)w / (GLfloat)h;
gluPerspective(35.0f, fAspect, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -400.0f);
}
void cleanup()
{
}
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27: //Escape key
cleanup();
exit(0);
}
}
/*-------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
printf("ci sono");
comm = MPI_COMM_WORLD;
MPI_Comm_size(comm, &sizeGlobale);
MPI_Comm_rank(comm, &my_rankGlobale);
if(my_rankGlobale==0)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (0, 0);
glutCreateWindow ("Marina");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
printf("dovrei stampare la finestra");
glutKeyboardFunc(handleKeypress);
glutMainLoop();
}
free(punti);
MPI_Finalize();
return 0;
} /* main */