Archive for February, 2009

Allocating Space for 2d Arrays with malloc()

Yet another post in my effort to be helpful. If you want to allocate space for a two dimensional array you need to be careful. Remember that a 2d array of floats is a float**, meaning a pointer to a block in memory which is a list of float pointers. Each of these float pointers is a pointer to an array of floats. If you want a couple functions to do this for you, you are in luck:

void allocate_matrix(float*** A, int n, int m) {
	int i;
	// Allocate storage for pointer array
	*A = malloc(n * sizeof(float *));
	if (A == NULL) {
		printf("Error: malloc could not allocate %d bytes for a\n", n
				* sizeof(float *));
		return;
	}

	for (i = 0; i < n; i++) {

	(*A)[i] = malloc(n * sizeof(float));
	if ((*A)[i] == NULL) {
		printf("Error: malloc could not allocate %d bytes for a\n", n
				* sizeof(float));
		return;
	}

	}
}

void free_matrix(float*** A, int n) {
	int i;
	for (i = 0; i < n; i++)
		free((*A)[i]);

	free(*A);
}

Remember that we need to take in a float*** because when we call the function we need to do this:

float** A;
allocate_matrix(&A, n, m);

Otherwise our pointer is copied and lost due to out of scope. Don’t forget that you need to free your 2d array the same way. This method is useful because gcc and many compilers have a limit on how much space you can just statically allocate. I found I can’t get much bigger than a 1500×1500 array without seg faulting at runtime. You might be able to increase the maximum size but the ideal way is to allocate the memory yourself.

 

Hope this way useful.

OpenMP and GCC Optimization

So I encountered and interesting little affect while compiling some C code in OpenMP using GCC. Seems that my code ran fine without any automatic optimization flags (-O2, -O3), but when I turned them on, went haywire. The sequential version wasn’t affected.

I tracked down the problem and found that it was actually do to my use of the private variable in the OpenMP directive. Here is a sample of the problem.

 

#pragma omp parallel
{
	#pragma omp for private(i, j, new_j, skip_next)                      	for(i = 0; i < n; i++)
        	{                                                                    	// Set each thread on one row of the loop
                	skip_next = 0;
                	for(j = 0; j < n; j++)                                       	{
				....

The problem actually occured because I did not have ’skip_next=0′ on line 5 of this code. This didn’t seem to matter when optimization was disabled but did cause incorrect semantics when I turned it on. Seems like OpenMP says you must assume that all privates are uninitialized when you start a new directive block. If you want them to be private but start with some default value, do what I did above or add the variable to the firstprivate() attribute instead of the private attribute:

 

#pragma omp for firstprivate(skip_next) private(i, j, new_j)

HF100 + ImageMixer Fix For Startup Freeze

So I just got a new camcorder thanks to now qualifying for the US stimulus package from back in April. I was looking around for something to help stimulate the economy and decided that I should get a camcorder while prices are low and money is high. Anyway, the HF100 is flash based and really nice. It records in full HD 1080p with pretty decent picture quality, though I have yet to record in full light setting (outside). So far I only have sort of dim shots indoors of my roommate’s cats and my ferret.

Anyway in my commitment to making a post with something useful every week, I’ve decided to share a tip with the world. Seems as though when I installed my ImageMixer 3 SE software, it would just freeze upon startup. Normally I wouldn’t mind the issue with crap proprietary software, but there is no easy way to edit .MTS/M2TS files without converting and losing quality. Since I prefer to keep the originals as well as edit them so that extra footage is chopped off, I needed to figure out what was wrong. After some digging I found the solution to my problem.

If this occurs for you, you need to install all third party codec packs you installed. I had the CCCP (Community Codec Pak), which is useful for watching MKVs. Unfortunately, that was messing up something, so I had to uninstall it. I found numerous posts talking about uninstalling codec packs, so that’s probably the way to go.

Unfortunately, that eliminates the ability to play back MKV’s in WMV. I don’t have a solution for this but I suggest installing VLC. It is wonderful and plays everything.

My first video, the high definition is better if you go straight to youtube:

 

Return top