#C
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/* Define an Ion type to hold the
coordinates of an Ion */
typedef struct Ion
{
double x;
double y;
double z;
} Ion;
/* The reference Ion */
struct Ion reference_ion;
/* Return the square of a number */
double square(double x)
{
return x*x;
}
/* Energy function to be mapped */
double calc_energy( struct Ion ion )
{
double r;
= sqrt( square( reference_ion.x - ion.x ) +
r ( reference_ion.y - ion.y ) +
square( reference_ion.z - ion.z ) );
square
/* The energy is simply 1 / r */
return 1.0 / r;
}
/* You will need to fill in this function to read in
an array of ions and return the array */
struct Ion* readArrayOfIons(int *num_ions)
{
int i;
*ions = (Ion*)malloc(10 * sizeof(Ion));
Ion
*num_ions = 10;
for (i=0; i<10; ++i)
{
[i].x = 0.0;
ions[i].y = 0.0;
ions[i].z = i;
ions}
return ions;
}
int main(int argc, char **argv)
{
int i, num_ions;
struct Ion *ions_array;
double total_energy, mapped_energy;
/* Lets put the reference ion at (1,2,3) */
.x = 1.0;
reference_ion.y = 2.0;
reference_ion.z = 3.0;
reference_ion
/* Now read in an array of ions */
= readArrayOfIons( &num_ions );
ions_array
= 0.0;
total_energy
#pragma omp parallel private(mapped_energy) \
reduction( + : total_energy )
{
= 0.0;
mapped_energy
#pragma omp for
for (i=0; i < num_ions; ++i)
{
/* Map this ion against the function */
+= calc_energy( ions_array[i] );
mapped_energy }
/* Reduce to get the result */
+= mapped_energy;
total_energy }
("The total energy is %f\n", total_energy);
printf
return 0;
}