Posts tagged ‘C++’
Intel Performance Libraries allow you to leverage both parallelism and SIMD instructions in C#
Modern microprocessors can execute Single Instruction, Multiple Data (SIMD) instructions. Because the execution units for SIMD instructions usually belong to a physical core, it is possible to run as many SIMD instructions in parallel as available physical cores. The usage of these vector-processing capabilities in parallel can provide important speedups in certain algorithms. You can use Intel Performance Libraries to leverage both parallelism and SIMD instructions in C# and .NET Framework 4. Read more
A project with an output type of class library cannot be started directly
Did you get bellow error message while compiling your ASP.NET project or your C# project in Visual Studio 2005 or Visual Studio 2008 ?
Error message:
A project with an Output type of Class Library cannot be started directly. In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.
To fix this issue, do these steps:
- Right click the Project name in Solution Explorer of Visual Studio
- Select Set as StartUp Project from the popup menu
- Re-run your project, it should work !
If it did not work, be sure that you have set your start page. If your project is C# Windows Application or C# Console Application, try this:
- Right click the Project name in Solution Explorer of Visual Studio
- Select Properties
- Select Application tab
- In Output type drop box, select the correct application type of your project
- Re-run your project and let me know if it won’t work.
Dynamic Memory Allocation for Two Dimensional Arrays in C++
In C++, a two-dimensional array can be declared simply like this
int a[10][20];
However, this declaration requires contanst array size and it is inconvenient when the array size vary. To avoid this issue, you can use dynamic two dimensional array. There are several ways to allocate a dynamic two-dimensional array in C++. In this post, I will show you three most frequent methods.
First one is flattening the two dimensional array into one dimensional array and using it as one dimensional array
//Allocate
int *a = new int[m*n];
//Use a[m][n]
for( int i = 0 ; i < m ; i++)
for ( int j = 0 ; j < n ; j++)
a[i*n + j] = 1;
//Deallocate
delete[] a;
Second method is using an array of dynamic one-dimensional arrays. It makes use of dynamic 2D array be easier. However, elements of array may not be continuous in memory
int **a = new int*[m];
for ( int i = 0 ; i < m ; i++)
a[i] = new int[n];
for ( int i = 0 ; i < m ; i++)
for ( int j = 0 ; j < n ; j++)
a[i][j] = 1;
for ( int i = 0 ; i < m ; i++) delete[] a[i];
delete[] a;
//Allocate
int **a = new int*[m];
int a[0] = new int[m*n];
for ( int i = 1 ; i < m ; i++) a[i] = a[i-1] + n;
//Use a[m][n]
for ( int i = 0 ; i < m ; i++)
for ( int j = 0 ; j < n ; j++)
a[i][j] = 1;
delete[] a[0];
delete[] a;
Example Code of Multithreading in C# GUI Application
One of my friends recently asked me about the multithreading in C# so that many jobs can work on a same shared data. Like many programming languages, C# has its own libraries to support this need. Let me firstly describe a little bit about multithreading.
In the trend of increasing number of cores integrated in the same processor chip, you may wonder “How do I utilize all available cores to make my program run faster?”. I guess that most of programs you’ve ever written till now are in sequential model. In this model, only one job is executed at a time and all jobs are finished in a sequential order. However, if you do a review of your programs and find some jobs can be run parallelly, multithreading can be a good choice to answer your question. Using a right multi-thread programming model can speed up your program if it runs on a machine with multi-processor/multi-core. However, this is not the only purpose of using many threads in a program. Another purpose of multithreading is interaction. For example, your program provides a service that may be requested by many clients at the same time. Instead of letting the clients queue and wait for their turns, the program can accept all requests and process them concurrently. At the result, all clients can receive the responses at the time of request.
A multi-thread program is enabled by using multithreading libraries such as OpenMP, Pthreads, Java Threads, Windows API threads… I don’t know exactly how many libraries of this type but I’m sure there are a lot. Some of them are created by companies like Microsoft, Intel while the others are born by research groups in universities or organizations. My professor developed her own one for writting programs running on Cray or similar machines. In practice, each programming language has one or many tools that support multithreading. So which one should I choose? It is very hard to answer this question since it depends on the usage purpose, computing model and the programming language of your program.
In C#, the System.Threading namespace gives you a number of types that enable multithreading programming. In this post, I did a small example program demonstrating how to do multithreading in C# GUI application. In the program, two threads are created and work on a same shared text data. One thread is scrolling in the text data from right the left and the other does the scrolling of same text in the reverse direction. Below is the program screenshot.
Download source code here