Click here to Skip to main content
15,889,216 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionCreate And Start Service using C++ Pin
sangamdumne23-Apr-12 23:36
sangamdumne23-Apr-12 23:36 
AnswerRe: Create And Start Service using C++ Pin
JohnCz24-Apr-12 2:59
JohnCz24-Apr-12 2:59 
GeneralRe: Create And Start Service using C++ Pin
sangamdumne24-Apr-12 22:57
sangamdumne24-Apr-12 22:57 
GeneralRe: Create And Start Service using C++ Pin
JohnCz24-Apr-12 23:33
JohnCz24-Apr-12 23:33 
GeneralRe: Create And Start Service using C++ Pin
sangamdumne25-Apr-12 20:19
sangamdumne25-Apr-12 20:19 
QuestionIndata validation char* input Pin
Johan Bertilsdotter23-Apr-12 22:21
Johan Bertilsdotter23-Apr-12 22:21 
QuestionRe: Indata validation char* input Pin
CPallini24-Apr-12 0:58
mveCPallini24-Apr-12 0:58 
QuestionIncorrect output of sum Pin
Ron120223-Apr-12 21:28
Ron120223-Apr-12 21:28 
Hi, I am using mpi and cuda which is basically like a c programming. Just confused about the pointers here. Some help would be appreciated.

my cuda code
#include <stdio.h>



__global__ void add(int *devarray, int *devsum)
{
        int index = blockIdx.x * blockDim.x + threadIdx.x;
        *devsum = *devsum + devarray[index];
}

extern "C"
int * run_kernel(int array[],int nelements)
{
        int  *devarray, *sum, *devsum;
        sum =(int *) malloc(1 * sizeof(int));
        
        printf("\nrun_kernel called..............");       
        
        cudaMalloc((void**) &devarray, sizeof(int)*nelements);
        cudaMalloc((void**) &devsum, sizeof(int));       
        cudaMemcpy(devarray, array, sizeof(int)*nelements, cudaMemcpyHostToDevice);
        //cudaMemcpy(devsum, sum, sizeof(int), cudaMemcpyHostToDevice);
        add<<<2, 3>>>(devarray, devsum);
      //  printf("\ndevsum is %d", devsum);		
	cudaMemcpy(sum, devsum, sizeof(int), cudaMemcpyDeviceToHost);

        printf(" \nthe sum is %d\n", *sum);
        cudaFree(devarray);
        cudaFree(devsum);
	return sum;
        
}

This function returns int * as sum.
my MPI code
#include "mpi.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define  ARRAYSIZE	2000

#define  MASTER		0



int  data[ARRAYSIZE];




int main(int argc, char* argv[])
{

int   numtasks, taskid, rc, dest, offset, i, j, tag1, tag2, source, chunksize, namelen; 

int mysum;

long sum;

int update(int myoffset, int chunk, int myid);
char myname[MPI_MAX_PROCESSOR_NAME];

MPI_Status status;
double start = 0.0, stop = 0.0, time = 0.0;
double totaltime;
FILE *fp;
char line[128];
char element;
int n;
int k=0;




/***** Initializations *****/

MPI_Init(&argc, &argv);

MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

MPI_Comm_rank(MPI_COMM_WORLD,&taskid); 
MPI_Get_processor_name(myname, &namelen);

printf ("MPI task %d has started on host %s...\n", taskid, myname);

chunksize = (ARRAYSIZE / numtasks);

tag2 = 1;

tag1 = 2;


/***** Master task only ******/

if (taskid == MASTER){

  fp=fopen("integers.txt", "r");
  if(fp != NULL){
   sum = 0;
   while(fgets(line, sizeof line, fp)!= NULL){
    fscanf(fp,"%d",&data[k]);
    sum = sum + data[k]; // calculate sum to verify later on
    k++;
   }
  }


printf("Initialized array sum %d\n", sum);

  /* Send each task its portion of the array - master keeps 1st part */

  offset = chunksize;

  for (dest=1; dest<numtasks; dest++) {

    MPI_Send(&offset, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);

    MPI_Send(&data[offset], chunksize, MPI_INT, dest, tag2, MPI_COMM_WORLD);

    printf("Sent %d elements to task %d offset= %d\n",chunksize,dest,offset);

    offset = offset + chunksize;

   }



  /* Master does its part of the work */

  offset = 0;
  mysum = run_kernel(&data[offset], chunksize);
  printf("Kernel returns sum %d", mysum);

  //mysum = update(offset, chunksize, taskid);



  /* Wait to receive results from each task */

  for (i=1; i<numtasks; i++) {

    source = i;

    MPI_Recv(&offset, 1, MPI_INT, source, tag1, MPI_COMM_WORLD, &status);

    MPI_Recv(&data[offset], chunksize, MPI_INT, source, tag2,MPI_COMM_WORLD, &status);

   }



  /* Get final sum and print sample results */  

  MPI_Reduce(&mysum, &sum, 1, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);



  printf("\n*** Final sum= %d ***\n",sum);



 }  /* end of master section */





/***** Non-master tasks only *****/



if (taskid > MASTER) {



  /* Receive my portion of array from the master task */
  start= MPI_Wtime();

  source = MASTER;

  MPI_Recv(&offset, 1, MPI_INT, source, tag1, MPI_COMM_WORLD, &status);

  MPI_Recv(&data[offset], chunksize, MPI_INT, source, tag2,MPI_COMM_WORLD, &status);

  mysum = run_kernel(&data[offset], chunksize);
  printf("\nKernel returns sum %d ", mysum);

// mysum = update(offset, chunksize, taskid);
  stop = MPI_Wtime();
  time = stop -start;
  printf("time taken by process %d to recieve elements and caluclate own sum is = %lf seconds \n", taskid, time);
  
 // totaltime = totaltime + time;



  /* Send my results back to the master task */

  dest = MASTER;

  MPI_Send(&offset, 1, MPI_INT, dest, tag1, MPI_COMM_WORLD);

  MPI_Send(&data[offset], chunksize, MPI_INT, MASTER, tag2, MPI_COMM_WORLD);



  MPI_Reduce(&mysum, &sum, 1, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);



  } /* end of non-master */



 MPI_Finalize();



}  

Now here mysum = run_kernel(&data[offset], chunksize);
gets the sum and stores into mysum.
Here mysum should be pointer, iguess but then
MPI_Reduce(&mysum, &sum, 1, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);
does not work properly and system throws memory error.

here is the output of above code -

MPI task 2 has started on host 4
MPI task 3 has started on host 4
MPI task 0 has started on host 4
MPI task 1 has started on host 4

Initialized array sum 9061
Sent 500 elements to task 1 offset= 500
Sent 500 elements to task 2 offset= 1000
Sent 500 elements to task 3 offset= 1500



run_kernel called..............
the sum is 10

Kernel returns sum 159300360 time taken by process 2 to recieve elements and caluclate own sum is = 0.290016 seconds
run_kernel called..............
the sum is 268452367
run_kernel called..............
the sum is 10

Kernel returns sum 145185544 time taken by process 3 to recieve elements and caluclate own sum is = 0.293579 seconds
run_kernel called..............
the sum is 1048

Kernel returns sum 156969736 time taken by process 1 to recieve elements and caluclate own sum is = 0.297599 seconds
Kernel returns sum 152148496
*** Final sum= 613604136 ***

The final sum and initialized sum is not matching.

Any idea what is going wrong?
Thanks
Question[SOLVED] Loading Bitmap Into Picture Control Pin
AmbiguousName23-Apr-12 18:57
AmbiguousName23-Apr-12 18:57 
AnswerRe: Loading Bitmap Into Picture Control Pin
enhzflep23-Apr-12 22:49
enhzflep23-Apr-12 22:49 
AnswerRe: Loading Bitmap Into Picture Control Pin
AmbiguousName23-Apr-12 23:28
AmbiguousName23-Apr-12 23:28 
AnswerRe: Loading Bitmap Into Picture Control Pin
SandipG 23-Apr-12 22:51
SandipG 23-Apr-12 22:51 
AnswerRe: Loading Bitmap Into Picture Control Pin
AmbiguousName23-Apr-12 23:31
AmbiguousName23-Apr-12 23:31 
GeneralRe: Loading Bitmap Into Picture Control Pin
enhzflep24-Apr-12 0:54
enhzflep24-Apr-12 0:54 
GeneralRe: Loading Bitmap Into Picture Control Pin
AmbiguousName24-Apr-12 1:02
AmbiguousName24-Apr-12 1:02 
QuestionThe entry point GetFileVersionInfoSizeEx Pin
jkirkerx23-Apr-12 13:23
professionaljkirkerx23-Apr-12 13:23 
AnswerRe: The entry point GetFileVersionInfoSizeEx Pin
Richard Andrew x6423-Apr-12 14:02
professionalRichard Andrew x6423-Apr-12 14:02 
GeneralRe: The entry point GetFileVersionInfoSizeEx Pin
jkirkerx23-Apr-12 15:10
professionaljkirkerx23-Apr-12 15:10 
GeneralRe: The entry point GetFileVersionInfoSizeEx Pin
JohnCz24-Apr-12 9:29
JohnCz24-Apr-12 9:29 
GeneralRe: The entry point GetFileVersionInfoSizeEx Pin
jkirkerx24-Apr-12 10:11
professionaljkirkerx24-Apr-12 10:11 
AnswerRe: The entry point GetFileVersionInfoSizeEx Pin
JohnCz25-Apr-12 1:15
JohnCz25-Apr-12 1:15 
GeneralRe: The entry point GetFileVersionInfoSizeEx Pin
jkirkerx25-Apr-12 6:11
professionaljkirkerx25-Apr-12 6:11 
GeneralRe: The entry point GetFileVersionInfoSizeEx Pin
JohnCz25-Apr-12 8:04
JohnCz25-Apr-12 8:04 
GeneralRe: The entry point GetFileVersionInfoSizeEx Pin
jkirkerx25-Apr-12 9:10
professionaljkirkerx25-Apr-12 9:10 
QuestionTabControl In MFC Pin
002comp22-Apr-12 23:59
002comp22-Apr-12 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.