Posts tagged ‘Tutorial’

This tutorial shows you how to download an HTML page, or any other type of web page, using C++ or C. This tutorial is only applicable for Windows programs, since the methods described here utilize a library written for Windows only. In this tutorial, we will be calling a function which will read a webpage, and save it to a file. After the file is created and saved, we can proceed to read that file through standard methods. At first glance, it may seem like this method is very inefficient, since hard drive accesses take a long time. But in actuality, the vast majority of the performance penalty will be from downloading the web page from the internet. Since the we read the file directly after creating it, you can be assured that the file is in cache, so there won’t be such a performance hit.

Step 1: Include and link the appropriate library

#include <urlmon.h>

Aside from including the library header file, you will need to link the urlmon.lib. To do this, right click on your project in the solution explorer windows, and select Properties from the pop-up menu. Go to the Configuration Properties -> Linker -> Input window. In the “Additional Dependencies” field, type urlmon.lib and press enter. Apply your changes, and close the project properties window.

Step 2: Choose Unicode or ASCII for your project

There are two types of character sets that can be used in an application. The first, ASCII, has only 8 bits, or 1 byte, per character. ASCII is often considered outdated, but is much simpler to deal with. Unicode uses more 16 bits per character, which facilitates muli-lingual programs. There are two sets of functions in the urlmon library, one set of functions is for ASCII, and the other set of functions is for Unicode. I have set the project in this tutorial to compile with the ASCII character set. You may choose to use Unicode, of course, but it just important that you know what character set your project is set to compile. To find out, open up the project properties window, and go to the Configuration Properties -> General window. Notice what the “Character Set” field is set to. “Not Set” corresponds to using the ASCII character set.

Step 3: Download the web page to a file

To download the web page, simply use the URLDownloadToFile function. This function returns an HRESULT error code, which is really just a long. When dealing with HRESULTs, just keep in mind that zero is returned as success. Therefore, it is always best to explicitly use the error code definitions, such as S_OK for success.

char webAddress[256];
char szFileName[80] = "result.html";

cout << "Please enter web address: ";	// example:
cin >> webAddress;

HRESULT hr = URLDownloadToFile(NULL, webAddress, szFileName,0, NULL);
if (hr == S_OK)
	cout << "Success!\n";
	// Open the file and print it to the console window
	// Since the file was just written, it should still be in cache somewhere.
	ifstream fin(szFileName);
	char szBuff[2048];
	while(fin.getline(szBuff, 2048))
		cout << szBuff << "\n";
	cout << "Operation failed with error code: " << hr << "\n";

Download the source code

You can download the source code for this tutorial here

Understanding how to use LockBits is essential for creating high performance GDI+ applications. Usually, GDI+ is thought of as a low performance graphics API. While arguments can be made for this, if you use GDI+ properly, you can achieve great performance. Continue reading ‘Using LockBits in GDI+’ »

Writing raw C++ code to read a JPG or JPEG image can be a cumbersome task. Fortunately, libraries exist which we can use to perform the dirty work for us. In this tutorial, we will be using the GDI+ interface to easily read image files and draw them onto the window. In you are unfamiliar with GDI+, I would suggest you take a look at my previous tutorial on how to get up and running with GDI+. Please note that this tutorial is aimed for Windows programs only, since GDI+ is not available on other platforms. Continue reading ‘How to load a JPG with C++’ »

This tutorial will focus on how to create and compile an application that uses GDI+. We will be starting a project from scratch using Microsoft Visual Studio. GDI+ is a powerful, object oriented API for doing mostly 2D graphics. Unlike GDI, GDI+ is generally much easier to use, much more difficult to misuse, and in many cases can produce higher quality images than GDI. While GDI+ is considered slower than GDI, it is still perfectly acceptable for most applications. Continue reading ‘Getting started with GDI+ in Visual Studio’ »

Atomic operations are often essential for multithreaded programs, especially when different threads need to access or modify the same data. Conventional multicore CPUs generally use a test-and-set instruction to manage which thread controls which data. CUDA has a much more expansive set of atomic operations. With CUDA, you can effectively perform a test-and-set using the atomicInc() instruction. However, you can also use atomic operations to actually manipulate the data itself, without the need for a lock variable. Continue reading ‘CUDA – Tutorial 5 – Performance of atomics’ »

The SSE instruction set can be a very useful tool in developing high performance applications. SSE, or Streaming SIMD Extensions, is particularly helpful when you need to perform the same instructions over and over again on different pieces of data. SSE vectors are 128-bits wide, and allow you to perform calculations for 4 different floating point numbers at the same time. SSE can also be configured to work on 2, 64-bit floating point numbers concurrently, 4, 32-bit integers, or even 16, 8-bit chars. Continue reading ‘Getting started with SSE programming’ »

There are several standard, cross platform ways to create high performance, multithreaded programs. There are no standard ways to spawn threads with the C++ language, which means that sometimes we have to resort to using compiler-specific methods to create threads for our programs. This tutorial will be focused on how to easily create work threads for your windows, or WIN32, program using Microsoft Visual Studio. Continue reading ‘Writing multithreaded programs for Windows’ »

This tutorial will discuss how to perform atomic operations in CUDA, which are often essential for many algorithms. Atomic operations are easy to use, and extremely useful in many applications. Atomic operations help avoid race conditions and can be used to make code simpler to write. Continue reading ‘CUDA – Tutorial 4 – Atomic Operations’ »

Until now, we have only talked about synchronous, blocking communications in MPI. This tutorial will focus on Asynchronous, non-blocking communication with MPI. Asynchronous communication is often the key to achieving high performance computing with MPI applications. Using asynchronous communication has several advantages. Continue reading ‘MPI – Tutorial 5 – Asynchronous communication’ »

This tutorial will be discussing how different threads can communicate with each other. In the previous tutorial, each thread operated without any interaction or data dependency from other threads. However, most parallel algorithms require some amount of data to be communicated between threads. Continue reading ‘CUDA – Tutorial 3 – Thread Communication’ »