Error in garbage collection stage?

I’m writing a program to sort ~100,000 elements. The program takes input in the form of files called aNUMBER.dat (a1000.dat, a40000.dat, etc) and outputs the sorted numbers, then min, max, and median.

My code is as follows:

[codebox]#include <cuda.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <sys/time.h>


#define TODEV cudaMemcpyHostToDevice

#define TOHOST cudaMemcpyDeviceToHost

//Optimization - a total of BLOCK_SIZE * BUBBLE_FACTOR elements are sorted by each block

#define BLOCK_SIZE 512 //Number of threads/block

#define BUBBLE_FACTOR 2 //Element count each thread bubbles over - must be greater than 1 to prevent conflict


#define MAX_BLOCKS

//takes the array, the offset, and the upper bound length of the array

global void BlockBubbleSort(int* a, int N)



int main(int argc, char** argv)


    if (argc < 2)


            printf("Error:  Must specify input file\n");



    //N is overall dimension of array to read in, determined by filename

    int N, i;


size_t MEM_SIZE = sizeof(int) * N; //determine array size

    size_t  THREAD_SIZE     = sizeof(int) * PER_BLOCK;

    int*    a_h             = (int*)malloc(MEM_SIZE);               //array for host

    int*    a_d;            cudaMalloc((void**)&a_d, MEM_SIZE);     //array for device

if (!a_h)


            printf("Error: Failed to allocate host memory\n");




    if (!a_d)


            printf("Error: failed to allocate device memory\n");


    //Read data into host array

    FILE* handle = fopen(argv[1],"r");

    if (!handle)


            printf("Error: invalid input file (file does not exist\n");



    for (i = 0 ; i < N ; i++) fscanf(handle, "%d", &a_h[i]);


//Execute kernel


    if (N%PER_BLOCK) BLOCK_COUNT += 1;

// cudaMemcpy(a_d, a_h, MEM_SIZE, TODEV);

// BlockBubbleSort<<<BLOCK_COUNT, BLOCK_SIZE, THREAD_SIZE>>>(a_d, N);

// cudaMemcpy(a_h, a_d, MEM_SIZE, TOHOST);

//deallocate resources


float median = 0;

int* merged = (int*)malloc(MEM_SIZE);//do merge on CPU;

    int* midx       = (int*)malloc(BLOCK_COUNT);

    if (!merged)


            printf("Error: Could not allocate merged block memory\n");




    for (i = 0 ; i < BLOCK_COUNT ; i++)

            midx[i] = PER_BLOCK * i;

    int idx = 0;

    int mmidx;

    while (idx < N)


            mmidx = -1;

            for (i = 0 ; i < BLOCK_COUNT ; i++)


                    if (midx[i] < N && midx[i] < PER_BLOCK * (1+i))

                            if (mmidx < 0)

                                    mmidx = i;

                            else if (a_h[midx[i]] < a_h[midx[mmidx]])

                                    mmidx = i;


            merged[idx] = a_h[midx[mmidx]];




    for (i = 0 ; i < N ; i++)

            printf("%d\n", merged[i]);

    printf("Min: %d\nMax: %d\n",merged[0],merged[N-1]);

    median = (float)merged[N/2];

    if (N&1 == 0)

            median = (median + (float)merged[N/2+1])/2.;

    printf("Median: %.1f\n",median);


    printf("a_h freed\n");


    printf("merged freed\n");


    printf("midx freed\n");



My output is:


midx freed

*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x0808afe8 ***

======= Backtrace: =========











======= Memory map: ========


While this is obviously not the code I actually use to sort, this is still giving me the error even with all of the business code stripped out. It goes away only when I don’t allocate the memory on the device, but the CUDA code runs fine still and the error only pops up after execution. Any idea what’s going wrong?

quick edit; I ssh’d to another machine (I’m doing this on hardware I don’t have direct control over) and with a ~4000 point input file there was no issue, but the 110,000+ file gave the same error.

run it through valgrind. you’re probably trampling host memory somewhere

I don’t have root on these boxes and valgrind’s not installed - any other suggestions until I can convince a professor to put it on?