Vectors or dynamic memory?

I am running simulations that require large amounts of data (too much for a standard array). If I’m going to use CUDA to parallelize the simulations, am I better off using vectors or dynamic memory (new etc.) to hold my data?

I’ve written this simple program as an example of what I’m trying to accomplish in serial:

#include <cmath>
#include <cstdlib>
#include <vector>
#include <cstdio>

#define XDIM 2000
#define YDIM XDIM

using namespace std;

void circle(vector<int> &f)
	int j(0);

	int n = XDIM*YDIM;

	int r((YDIM / 3));

	int centre[2] = { (XDIM / 2),(XDIM / 2) };

	for (j = 0; j < XDIM*YDIM; j++)
		if (((j%XDIM) - centre[0])*((j%XDIM) - centre[0]) + ((j - j%XDIM) / XDIM - centre[1])*((j - j%XDIM) / XDIM - centre[1]) < (r + 1)*(r + 1) && ((j%XDIM) - centre[0])*((j%XDIM) - centre[0]) + ((j - j%XDIM) / XDIM - centre[1])*((j - j%XDIM) / XDIM - centre[1]) >= (r - 1)*(r - 1))
		{ = 1;

void main()
	int j(0), x(0), y(0);
	vector<int> field(XDIM*XDIM, 0);


	FILE * f;

	if (fopen_s(&f, "circle.dat", "a") != 0)

		printf("I could not open the file circle.dat\n");
		printf("Aborting now.\n");

	for (j = 0; j < XDIM*YDIM; j++)

		if ( == 1)
			x = j%XDIM;
			y = (j - j%XDIM) / XDIM;

			fprintf(f, "%d\t%d\n\n", x, y);


Any help you could offer would be appreciated.


putting cuda a side a little, if you need to speed up your code is much better use fixed arrays that dynamic arrays, the problem with vectors is that they use fixed arrays, and when they are without space, they usually create another array doubling the actual capacity and copying the actual data to the new one. if you can store your data in a vector then u could store it in an fixed array.

Now, looking at your code, i see u work on small parts of the data. u can actually work in small segments of the array. in example, if you have 2 millions points (?), you can work in the first 10.000 points, then in the next 10.000, and keep going until you finish. that’s something you can actually parallelize using cuda.

No matter what, I think the wisest thing is to always choose RAII :P