d_new_input_2d = (cl_mem *)malloc( sizeof(cl_mem) * num_devices); // line 40
d_new_input_2d[icount] = clCreateBuffer(context, CL_MEM_READ_WRITE,
max_size * 5 * sizeof(unsigned int), NULL, &ret); // line 55
each element size = max_size * 5 * sizeof(unsigned int)
so max_size * 5 * sizeof(unsigned int) ( > or = or < ) sizeof(cl_mem) ;
To be honest, you must be careful about the memory size, especially in your code, each loop will allocate memory space.
Look at line 135, kernel[icount] = clCreateKernel(program, "Kernel_name", &ret); the kernels are the same one?
I'm not sure what caused the result, what's the error message?