>> Single Point of Failure >>


Recent Entries



Popular Tags

Art Blog Blogging Book Books C++ C/C++ Calvin Cartoon Complaints Daughter Definition Development Event Experiment Family Favorites Firefox Focus Fun Google Humor Introspection Life Linux Movie Movies Music Organization Photo Photography Programming Python Reading Software Tips Tools Travel TV Ubuntu Vacations Video Water White Work

Caution: Highly Addictive

Ξ December 29th, 2011 | → 0 Comments |
TV |, , , , , |


 


White Christmas

Ξ December 24th, 2011 | → 0 Comments |
Family |, , , |


 


Mug Collection

Ξ December 23rd, 2011 | → 0 Comments |
Fun |, , , , |


 


Going Concurrent

Ξ December 23rd, 2011 | → 0 Comments |
Programming |, , , , |


I had to implement a progress bar for a GUI application, and in order to keep the application “live” my focus during last couple of weeks shifted to multi-threaded concurrent programming. The solution was simple and it basically implied implementing a background thread to update the progress bar while filling a data structure by reading a file. It was simple enough to be implemented in a couple of days, but it became clear that some of the topics related to concurrent programing just weren’t as consolidated I would like.

Since the I’ve been skimming over some interesting articles on the subject. Also began to read “Concurrent Programming in Java, 2nd Edition” by Doug Lea and “The Art of Concurrency” by Clay Breshears .

The following is my attempt to solve the brain-number problem (addressed initially by Ari Sundaram).

#include <cmath>
#include <iostream>
#include <limits>
#include <cassert>

#include "omp.h"

/* ***
 * Count the digits in the number 
 * ***/
int count_digits(int number)
{
	return log10(static_cast<double>(number)) + 1;
}

/* ***
 * Calculate the brain-value of the number
 * 
 * The brain-value is the sum of each digit in number to the power of the
 * number of digits.
 * BV(d1d2d3...dn) = d1^n + d2^n + d2^n + ...  + dn^n,
 *                                              where dn is the nth digit
 * ***/
int
brain_value(int number)
{
	int sum = 0;
	int digits = count_digits(number);
	
	while(number > 0)
	{
		sum += ::pow(number % 10, digits);
		number = number / 10;
	}

	return sum;
}

/* ***
 * Checks if number is a brain-number
 * 
 * A number is a brain-number when it's brain-value equals the number itself
 * ***/
bool
is_brain_number(int number)
{
	return brain_value(number) == number;
}

/* ***
 * Prints the brain-numbers in the lower/upper range
 *                                 (Serial Version)
 * ***/
void
print_brain_numbers_in_range(int lower, int upper)
{
	assert(lower >= 0);
	assert(lower <= upper);
	
	for(int i = lower; i <=upper; i++)
		if(is_brain_number(i))
			std::cout << i << std::endl;
}

/* ***
 * Prints the brain-numbers in the lower/upper range
 *                                 (Parallel Version, using OpenMP)
 * ***/
void
print_brain_numbers_in_range_parallel(int lower, int upper)
{
	assert(lower >= 0);
	assert(lower <= upper);

	#pragma omp parallel
	{
		#pragma omp for
		for(int i = lower; i <=upper; i++)
			if(is_brain_number(i))
				#pragma omp critical
				{
					// Critical section ensures correct output
					std::cout << i << std::endl;
				}
	}
}

/* ***
 * Helper function to test the 
 * ***/
template<typename Printer>
void execute_print_brain_numbers(Printer& printer, int lower, int upper) {
	
	std::cout << "The brain numbers between "
	          << lower << " and " << upper << " are:" << std::endl;
	double start = omp_get_wtime();
	printer(lower, upper);
	double end = omp_get_wtime();
	std::cout << "Listed in " << (end - start) << " seconds" << std::endl;
}

int main(void)
{
	execute_print_brain_numbers(
		print_brain_numbers_in_range,
			0,
			std::numeric_limits<int>::max() / 100);

	execute_print_brain_numbers(
		print_brain_numbers_in_range_parallel,
			0,
			std::numeric_limits<int>::max() / 100);

	return 0;
}

 


Today’s Soundtrack

Ξ December 20th, 2011 | → 0 Comments |
Music |, , , , |


 


Just had a nice time with volatile!

Ξ December 17th, 2011 | → 0 Comments |
Programming |, , , |


I’ve never had the chance (or in fact the need) to investigate the use of the volatile keyword in C/C++. Today I found some very interesting articles about the subject.

 - Andrei Alexandrescu’s article “volatile: The Multithreaded Programmer’s Best Friend” at Dr. Doobs’s.
Explains that using volatile prevents compiler optimizations that might render code incorrect in the presence of certain asynchronous events. And presents a technical discussion on how to use the volatile qualifier to create critical sections and avoid race conditions.

 - Arch Robison’s “Volatile: Almost Useless for Multi-Threaded Programming” at Intel Software Blogs.
Provides an example where volatile does not work, and defends that volatile does not provide a sound base to create correct multi-threaded programs. The discussion that follows is very interesting, spawning comments for several years, and including several very interesting references to how the C++ communities discussed/evolved volatile understanding.

 - Nigel Jones’ “Introduction to the Volatile Keyword” (here) and San Saks’ “Use voatile Judiciosly” (here).
Show interesting examples of what is and how to use the
volatile qualifier, providing examples of use in the scope of embedded systems development.

         


        So true… been there!

        Ξ December 13th, 2011 | → 0 Comments |
        Cartoon |, , |


        credits here.

         


        Chinese proverb

        Ξ December 10th, 2011 | → 0 Comments |
        Life |, , |


         

        “He who asks is a fool for five minutes, but he who does not ask remains a fool forever.”

         


        On the nightstand...