C++ Use in High Performance Computing

Filed in Programming Leave a comment

PGI Compilers and CUDA 5

Filed in Programming Leave a comment

nVidia_TESLA GPU

nVidia TESLA GPU (Photo credit: Philippe Lin)

I’ve been using the PGI Compilers along with CUDA to do GPGPU development for some time. The compilers themselves are fantastic – OpenACC support, OpenMP support and loads of other cool things.

Recently I upgraded to CUDA 5.0 and noticed that a number of programs now failed to compile, as the compiler wasn’t seeing CUDA 5 correctly. After a while searching the docs, searching the errors and doing big dirty greps, I eventually stumbled upon the answer.

To override what the PGI compiler thinks the correct CUDA version is, simple create a file called .mypgirc in your home directory with the line set DEFCUDAVERSION=5.0;

A nice one liner is;

echo "set DEFCUDAVERSION=5.0;" > ~/.mypgirc

 

Scarface as Code

Filed in Programming Leave a comment

#include <stdio.h>
#include <string.h>
 
void main(void){
 
   int tonyMontana=0;
   char* get="First";
 
   char* then=", then";
   printf("%s",get);
 
   while (!strcmp(get,"women\n")==0){
 
      switch (tonyMontana){
      case 1:
         get="power";
         printf("%s", then);
         break;
      case 2:
         get="women\n";
         printf("%s", then);
         break;
      default :
         get="money";
         break;
      }
      tonyMontana++;
      printf (" you get the %s",get);
 
   }
}

2012 Reading List

Filed in Programming Leave a comment

My reading list has really spiralled out of control lately, so this is a post to let you know what I’ll be reading for the rest of the year.

Cover of "Programming Python"

Programming Python

Python

Learning Python by Mark Lutz

Programming Python by Mark Lutz

Python Cookbook by Alex Martelli, Anna Ravenscroft and David Ascher

Python in a Nutshell (In a Nutshell (O’Reilly)) by Alex Martelli

 

Sysadmin’ing

The Practice of System and Network Administration by Thomas A. Limoncelli, Christina J. Hogan and Strata R. Chalup

Unix and Linux System Administration Handbook by Evi Nemeth, Garth Snyder, Trent R. Hein and Ben Whaley

 

Cover of "The Pragmatic Programmer: From ...

Cover via Amazon

Programming

Programming Pearls (ACM Press) by Jon Bentley

The Pragmatic Programmer by Andrew Hunt and David Thomas

Odds and Ends

OpenGL SuperBible: Comprehensive Tutorial and Reference by Richard S. Wright Jr., Nicholas S Haemel, Graham Sellers and Benjamin Lipchak

Classic Shell Scripting: Hidden Commands that Unlock the Power of Unix by Arnold Robbins and Nelson H. F. Beebe

Inventing on Principle

Filed in Programming Leave a comment

Every aspect of your life is a choice. There are default choices: you can choose to sleepwalk through your life and accept the path that’s laid out for you. You can choose to accept the world as it is… but you don’t have to. If there is something in the world you feel is wrong, and you have a vision of what a better world would be, you can find your guiding principle, and you can fight for a cause. […] Think about what matters to you, what you believe in, and what you might fight for.

Bret Victor

Found this under my desk

Filed in Programming 4 Comments

The ‘Anti-Java’ Professor and the Jobless Programmers

Filed in Programming 3 Comments

James Maguire’s article raises some interesting questions as to why teaching Java to first year CS / IT students is a bad idea. The article mentions both Ada and Pascal – neither of which really “took off” outside of the States, with the former being used mainly by contractors of the US Dept. of Defense.

This is my own, personal, extension to the article – which I agree with – and why first year students should be taught C in first year. I’m biased though, I learned C as my first language and extensively use C or C++ in projects.

Java is a very high level language that has interesting features that make it easier for programmers. The two main points, that I like about Java, are libraries (although libraries exist for C / C++ ) and memory management.

Libraries

Libraries are fantastic. They offer an API and abstract a metric fuck tonne of work that a programmer doesn’t care about. I don’t care how the library works inside, just that I have a way of putting in input and getting expected output (see my post on abstraction). I’ve extensively used libraries, even this week, for audio codec decoding. Libraries mean not reinventing the wheel and reusing code (something students are discouraged from doing, as it’s plagiarism, yet in the real world you are rewarded). Again, starting with C means that you appreciate the libraries more.

Memory Management

Managing your programs memory manually is a pain in the hole. We all know this after spending countless hours finding memory leaks in our programs. Java’s inbuilt memory management tool is great – it saves me from having to do it. However, if I had have learned Java first, I would assume (for a short amount of time) that all languages managed memory for you or that all languages were shite compared to Java because they don’t manage memory for you. Going from a “lesser” language like C to Java makes you appreciate the memory manager

What’s so great about C?

In the context of a first language to teach students, C is perfect. C is

  • Relatively simple
  • Procedural
  • Lacks OOP features, which confuse freshers
  • Low level
  • Fast
  • Imperative
  • Weakly typed
  • Easy to get bugs

Java is a complex language that will spoil a first year student. However, as noted, CS / IT courses need to keep student retention rates high. As an example, my first year class was about 60 people, final year was 8. There are ways to keep students, possibly with other, easier, languages in the second semester of first year – so that students don’t hate the subject when choosing the next years subject post exams.

Conversely, I could say that you should teach Java in first year and expand on more difficult languages like C or assembler (which should be taught side by side, in my mind) later down the line – keeping retention high in the initial years, and drilling down with each successive semester to more systems level programming.

There’s a time and place for Java, which I believe is third year or final year. This will keep Java fresh in the students mind while they are going job hunting after leaving the bosom of academia. This will give them a good head start, as most companies are Java houses in Ireland.

Abstraction

Filed in Programming 1 Comment

A few things can confuse programming students, or new people to programming. One of these is abstraction.

Wikipedia says:

In computer scienceabstraction is the process by which data and programs are defined with a representation similar to its meaning (semantics), while hiding away the implementation details. Abstraction tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. A system can have several abstraction layers whereby different meanings and amounts of detail are exposed to the programmer. For example, low-level abstraction layers expose details of the hardware where the program is run, while high-level layers deal with the business logic of the program.

That might be a bit too wordy for some people, and not at all clear. Here’s my analogy of abstraction.

Abstraction is like a car

A car has a few features that makes it unique.

  • A steering wheel
  • Accelerator
  • Brake
  • Clutch
  • Transmission (Automatic or Manual)

If someone can drive a Manual transmission car, they can drive any Manual transmission car. Automatic drivers, sadly, cannot drive a Manual transmission drivers without “relearing” the car. That is an aside, we’ll assume that all cars are Manual transmission cars – as is the case in Ireland for most cars.

Since I can drive my car, which is a Mitsubishi Pajero, that means that I can drive your car – a Honda Civic, Toyota Yaris, Volkswagen Passat.

All I need to know, in order to drive a car – any car – is how to use the breaks, accelerator, steering wheel, clutch and transmission. Since I already know this in my car, I can abstract away your car and it’s controls.

I do not need to know the inner workings of your car in order to drive it, just the controls. I don’t need to know how exactly the breaks work in your car, only that they work. I don’t need to know, that your car has a turbo charger, only that when I push the accelerator, the car moves. I also don’t need to know the exact revs that I should gear up or gear down (although that would be better on the engine!)

Virtually all controls are the same. Standardization means that the clutch, break and accelerator are all in the same place, regardless of the car. This means that I do not need to relearn how a car works. To me, a car is just a car, and is interchangeable with any other car.

Abstraction means not caring

As a programmer, or someone using a third party API (for example), abstraction means not caring how the inner workings of some function works – Linked list data structure, variable names inside the function, the sorting algorithm used, etc – just that I have a standard (preferable unchanging) interface to do whatever I need to do.

Abstraction can be taught of as a black box. For input, you get output. That shouldn’t be the case, but often is. We need abstraction so that, as a programmer, we can concentrate on other aspects of the program – this is the corner-stone for large scale, multi developer, software projects.

Abstraction is not like Peter Sarstedt’s Where Do You Go To My Lovely.

You talk like Marlene Dietrich
And you dance like Zizi Jean-Maire
Your clothes are all made by Balmain
And there’s diamonds and pearls in your hair

You live in a fancy apartment
Off the Boulevard St. Michel
Where you keep your Rolling Stones records
And a friend of Sasha Distel.

You go to the embassy parties
Where you talk in Russian and Greek
And the young men who move in your circles,
They hang on every word you speak.

But where do you go to my lovely…
When you’re alone in your bed.
Tell me the thoughts that surround you.
I want to look inside your head.

I’ve seen all your qualifications
That you got from the Sorbonne
And the painting you stole from Picasso.
Your loveliness goes on and on.

When you go on your summer vacation,
You go to Juan-les-Pins
With your carefully designed topless swimsuit

You get an even suntan
On your back and on your legs
And when the snow falls you’re found in St. Moritz
With the others of the jet set

And you sip your Napoleon brandy,
But you never get your lips wet

But where do you go to my lovely…
When you’re alone in your bed.
Tell me the thoughts that surround you.
I want to look inside your head.

You’re in between twenty and thirty,
A very desirable age.
You’re body is firm and inviting,
But you live on a glittering stage.

Your name it is heard in high places.
You know the Agha Khan.
He sent you a race horse for Christmas
And you keep it just for fun, for a laugh, aha aha.

They say that when you get married,
It will be to a millionaire.
But they don’t realize where you came from
And I wonder if they really care, or give a damn.

But where do you go to my lovely…
When you’re alone in your bed.
Tell me the thoughts that surround you.
I want to look inside your head.

Ah, remember the back streets of Naples,
Two children begging in rags
Both touched with a burning ambition
To shake off their lowly-born tags, so they try.

So look into my face Marie-Claire
And remember just who you are.
Then go and forget me forever
But I know you still bear the scar deep inside, yes, you do.

Ah, I know where you go to my lovely…
When you’re alone in your bed.
I know the thoughts that surround you,
Cause I can look inside your head.

 

When You Write Your Essays in Programming Languages

Filed in Programming Leave a comment

Mutex vs. Semaphore

Filed in Programming Leave a comment

Back when I taught C programming labs, a student asked what was the difference between a Mutex and a Semaphore. My simple explination is below. Little did I know, Niclas Winquist came up with the same explanation in 2005. To me, this really is the best explanation for a non-technical audience.

Mutex

Mutex is also called “Mutual Exclusion”. The explanation is in the name, really. A Mutex is a bit like a bathroom on an airplane; cramped and piss on the floor.

  • You enter the bathroom
  • You lock the door
  • You do your business
  • You unlock the door
  • You leave the bathroom

Unless you are incredibly lucky or incredibly stupid, two people CANNOT be in the bathroom at the same time.

Semaphore

A Semaphore (flag) is a lot like bathrooms in a fancy hotel; nice heated seats and proper toilet paper. Let’s assume that there are 4 cubicles.

  • Andrew enters and locks his cubicle door
  • 3 Cubicle’s are free
  • Bob enters and locks his cubicle door
  • 2 Cubicle’s are free
  • Chris enters and locks his door
  • 1 Cubicle is free
  • David enters cubicle and locks his door
  • No Cubicles are free
  • Edward waits
  • Bob is finished and leaves the cubicle – unlocking it
  • Edward enters and locks his door

What this means is, if there are N  toilets and M people where M > N, someone will have to wait until there is a free toilet before they can proceed.

TOP