NotesIntelFortran
=====================
Contents
=========
msvcrtd.dll issue
dumpbin, editbin, stack
Fortran routines for DLL
Example Fortran routines for DLL called by C#
Setup IMSL
Setup MKL
Setup - Intel Fortran / IMSL Environment Variables
Intel Fortran 10.1 and VS. Net 2005
Managed Code
BLAS, IMSL and MKL
Building DLLs (Fortran DLLs used in Fortran apps)
!DEC$ ATTRIBUTES directives
Passing module variables and functions in DLL
Best Practice
Errors - Debugging
How to Add Version and other Metadata to DLL or EXE
Using VTune
Compiler Options
Build Macros (eg $(OUTDIR))
Using MKL
Using LAPACK95 & General Comment on DLLs, LIBs, Mod files
Mixed language programming
Stack Checking
Enable Vectorization and Report
Enable OpenMP
Using Thread Profiler
Profile Guided Optimization
Intel Array Visualizer
msvcrtd.dll issue
====================
***************
Performance Tools for Software Developers
libmmdd.dll is dependent on msvcrtd.dll which is no longer distributed.
Symptom(s):
Note: This only applies to the compilers for Intel? Extended Memory 64 Technology and for the Itanium? Architecture.
Applications or DLL's that are built with /MDd or directly link against Intel's libmmdd.dll may emit the runtime error.
This application has failed to start because msvcrtd.dll was not found. Re-installing the application may fix this problem.
Cause:
The Platform SDK distributed with Microsoft* Visual Studio* 2005 does not contain msvcrtd.dll. Using /MDd links against the Intel math library libmmdd.dll which has a dependency on msvcrtd.dll.
Solution:
This is a known issue that may be resolved in a future product release. As a work-around, use the msvcrtd.dll distributed with the Microsoft* Platform SDK available at http://www.microsoft.com/downloads/details.aspx?FamilyId=0BAF2B35-C656-4969-ACE8-E4C0C0716ADB&displaylang=en ? .
***************
May need to get msvcrtd.dll from somewhere to be put into c"\windows\system32"
dumpbin, editbin, stack
========================
To run these command line tools,
- go to "Start" button -> "All Programs"
-> "Intel Software Development Tools"
-> "Intel Compiler 8.0"
-> "Build Environment for Fortran IA-32 Applications"
To check the stack size of a program.
Run "dumpbin /headers executable_file", and you can see the "size of stack reserve" information in "optional header values".
To enlarge the stack of a program:
Run "editbin /STACK:<size in bytes> program.exe"
Alternatively
http://www.atalasoft.com/cs/blogs/rickm/archive/2008/04/22/increasing-the-size-of-your-stack-net-memory-management-part-3.aspx
The Easiest Way ( .NET 2.0 )
In .NET 2.0 and newer you can simply specify thread size in a thread?s constructor. Unfortunately, this method is only compatible only with Windows XP and newer operating systems. You can specify this parameter on those platforms but it will have no effect; the stack size in the binary header will be used.
using System.Threading;
?
Thread T = new Thread(threadDelegate, stackSizeInBytes);
T.Start();
Fortran routines for DLL
=========================
<Example>
Interface
subroutine my_sub(I)
!DEC$ ATTRIBUTES C, ALIAS:"_My_Sub" :: my_sub
integer i
end subroutine
end interface
</Example>
- Case Sensitive: Fortran is not, C/C++ is.
- Arrays are always passed by reference
- ATTRIBUTES for a routine may be: C, STDCALL, REFERENCE, VARYING
- ATTRIBUTES for an argument may be: VALUE, REFERENCE
- C or STDCALL makes passing all arguments by value, except arrays.
- the VALUE or REFERENCE argument options, overide the routine option
of C or STDCALL.
- for IA-32 system, need to put underscore for routine to be called by C.
- cannot call internal procedures from outside the program unit that contains them.
- To pass Variable number of arguments, need C and VARYING, not STDCALL
Example Fortran routines for DLL called by C#
================================================
! Public wrapper for status_msg_get_code
integer*4 pure function StatusMsgGetCode(msg)
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'_StatusMsgGetCode' :: StatusMsgGetCode
!DEC$ ATTRIBUTES REFERENCE :: msg
StatusMsgGetCode = status_msg_get_code(msg)
- uses STDCALL (can't handle optional argument (see Intel F User Guide)
- uses alias with leading underscore
- wrap and rename code to get rid of underscore in function name, eg.
status_msg_get_code --> StatusMsgGetCode
- uses REFERENCE to pass arguments
Setup IMSL
===========
Documentation -
1. Start -> Programs -> IMSL Fortran Library 5.0. This contains:
QuickStart, Readme, User's Guide
2. PDF docs contains
Math Library V1, V2, Statistical Libraries, Special Functions
IMSL is not Thread safe. It is still safe to use, provided that calls to the
IMSL routines are made from a single thread.
VS.Net integration
1. In VS.Net, goto Tools -> Options -> Intel(R) Fortran -> Project Directories ->
type in the Include and Libraries directory path.
2. Specify the following include statements;
include 'link_f90_static.h'
include 'link_f90_dll.h'
include 'link_f90_static_smp.h'
include 'link_f90_dll_smp.h'
or go to Projects -> Add Existing Item ... browse to add the library.
The link*.h files contain directives to point to certain *.dll files. For example,
link_f90_dll.h contents are:
!dec$objcomment lib:'imsl_dll.lib'
!dec$objcomment lib:'imslscalar_dll.lib'
!dec$objcomment lib:'imslblas_dll.lib'
3. Inside the code, in addition to the include directives in step 2, need to include
some USE statements. For example, to use the random number generator rnun, we need:
i) use rnun_int; or
ii) use imsl_libraries; or
iii) use numerical_libraries
iii - is used to provide backward compatibility with previous IMSL libraries and Fortran77
version of the library. It may not be necessary to use iii and calling the functions as before
will continue to work.
Using ii provides access to all the IMSL functions, so individual use statements are not needed.
However, some may choose to use i because it shows explicitly which functions are called.
Using BLAS
1. Intel MKL Blas library used automatically when IMSL is linked with the
SMP (ie. multiprocessing) option.
2. See ia32 or ia64 Readme to link 3rd party blas with IMSL.
IMSL version 6.0
- IMSL is now THREAD SAFE
- Env Var - run ia32\bin\fnlsetup.bat .
- MUST remove old references, eg. include 'link_f90_dll.h' (because new headers have diff name)
- MUST rename directory of older installations of IMSL, so that any old env vars cannot
accidentally point to it.
- Add include statement in the relevant source files:
include 'link_fnl_shared.h' ! for dynamic dlls
include 'link_fnl_shared_hpc.h' ! for dynamic dlls and SMP (OpenMP)
- Add include directory in VS.Net
Project - Properties - Fortran - Include Directories: $(FNL_DIR)\ia32\include\dll
- Add library directory in VS.Net
Project - Properties - Fortran - Library Directories: $(FNL_DIR)\ia32\lib
- Run the ASSURANCE tests provided by IMSL in ...\examples\eiat. Note that
in run_test.bat, need to use %LINK_FNL_STATIC%
Setup MKL
==========
Linking to MKL can be done either statically *.lib or dynamically *.dll
For ia32 apps, when linking statically, link to mkl_c.lib or mkl_s.lib
For ia32 apps, when linking dynamically, link to these STATIC libs:
mkl_c_dll.lib or mkl_s_dll.lib
that will provide interfaces to the correct DLLs.
For MKL v 10.0
- Major changes, MKL divided into layers: Interface, Threading, Computation, RTL.
- Support for 64bit via ILP64/LP64.
- Use of OpenMP for threading and MPI and Scalapack for distriubuted computing.
- Env Vars: The following variables would have been set by running tool/environment/mklvars32.bat
$(MKLPATH) = root location of MKL directory - eg D:\programs\Intel\MKL\10.0...
lib=$(MKLPATH)\ia32\lib
include=$(MKLPATH)\include
bin=$(MKLPATH)\ia32\bin
LIBRARY_PATH=$(MKLPATH)\ia32\lib
CPATH=$(MKLPATH)\include
FPATH=$(MKLPATH)\include
- Visual Studio config
Project -> Properties -> Linker -> General -> Add additional Library Directories
$(MKLPATH10)\ia32\lib
Project -> Properties -> Linker -> General -> Add additional Include Directories
$(MKLPATH10)\include
$(MKLPATH10)\interfaces\lapack95
- Linking
Intel advises to link libguide and libiomp dynamically even if others are linked statically.
Link items to consider:
Interface: Threading: Computation: RTL: Description:
mkl_intel_c_dll.lib mkl_sequential_dll.lib mkl_core_dll.lib <none> Dynamic, non-parallel, 32bit
Actual linking done in code by using the !$dec attributes such as:
!dec$objcomment lib:'mkl_intel_c_dll.lib'
!dec$objcomment lib:'mkl_sequential_dll.lib'
!dec$objcomment lib:'mkl_core_dll.lib'
!dec$objcomment lib:'mkl_lapack95.lib'
- You are advised to link with libguide and libiomp dynamically even if other libraries are
linked statically. (MKL user guide, Chap 5)
- To use THREADED / PARALLEL / OPENMP Intel MKL, it is highly recommended to compile your code with the /MT
option. The compiler driver will pass the option to the linker and the latter will load
multi-thread (MT) run-time libraries.
- For multi-threading based on Intel OpenMP
Interface:
lib\mkl_intel_c_dll.lib
Threading:
lib\mkl_intel_thread_dll.lib,
bin\mkl_intel_thread.dll,
Computation:
lib\mkl_core_dll.lib,
(many many bins....)
RTL:
lib\libguide40.lib, OR lib\libiomp5md.lib,
bin\libguide40.dll, OR bin\libiomp5md.dll
Setup - Intel Fortran / IMSL Environment Variables
=====================================================
user defined:
INCLUDE
C:\Program Files\VNI\CTT5.0\CTT5.0\INCLUDE\IA32;C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
LIB
C:\Program Files\VNI\CTT5.0\CTT5.0\LIB\IA32;C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\
PATH
C:\Program Files\VNI\CTT5.0\CTT5.0\LIB\IA32;%PATH%;d:\DATA\UsercheeOnD\tools\NixTools\bin;%MSNET_C%\bin;C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE;C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin;%PStill%;D:\Program\UnderstandF90\bin\pc-win95
system variables:
CTT_DIR
C:\Program Files\VNI\CTT5.0\CTT5.0\LIB\IA32;%PATH%;d:\DATA\UsercheeOnD\tools\NixTools\bin;%MSNET_C%\bin;C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE;C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin;%PStill%;D:\Program\UnderstandF90\bin\pc-win95
CTT_EXAMPLES
"C:\Program Files\VNI\CTT5.0\CTT5.0\examples\IA32"
CTT_FORTRAN_COMPILER
Intel(R) Fortran Compiler for 32-bit applications, Version 8.1
CTT_OS_VERSION
Microsoft Windows XP/2000/2003
F90
ifort
F90FLAGS
/w /I:"C:\Program Files\VNI\CTT5.0\CTT5.0\include\IA32" /fpe:3 /nologo
FC
ifort
FFLAGS
/w /I:"C:\Program Files\VNI\CTT5.0\CTT5.0\include\IA32" /fpe:3 /nologo
FP_NO_HOST_CHECK
NO
INCLUDE
C:\Program Files\VNI\CTT5.0\CTT5.0\INCLUDE\IA32;%INTEL_FORTRAN80%\ia32\include;C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\include\
INCLUDE_DIR
"C:\Program Files\VNI\CTT5.0\CTT5.0\include\IA32"
INTEL_FORTRAN80
C:\Program Files\Intel\Fortran\Compiler80
INTEL_LICENSE_FILE
C:\Program Files\Common Files\Intel\Licenses
KMP_DUPLICATE_LIB_OK
TRUE
LIB
C:\Program Files\VNI\CTT5.0\CTT5.0\LIB\IA32;%INTEL_FORTRAN80%\ia32\lib;C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Lib\
LIB_ARCH
IA32
LINK_F90
imsl_dll.lib imslscalar_dll.lib imslblas_dll.lib
LINK_F90_DLL
imsl_dll.lib imslscalar_dll.lib imslblas_dll.lib
LINK_F90_DLL_SMP
/Qopenmp /F6000000 /fpp imsl_dll.lib imslsmp_dll.lib mkl_c_dll.lib /link /nodefaultlib:libc.lib
LINK_F90_SMP
/Qopenmp /F6000000 /fpp imsl_dll.lib imslsmp_dll.lib mkl_c_dll.lib /link /nodefaultlib:libc.lib
LINK_F90_STATIC
imsl.lib imslscalar.lib imslblas.lib imsls_err.lib
LINK_F90_STATIC_SMP
/Qopenmp /F6000000 /fpp imsl.lib imslsmp.lib mkl_c_dll.lib imsls_err.lib /link /nodefaultlib:libc.lib
Path
d:\Program\Intel\VTune\CGGlbCache;d:\Program\Intel\VTune\Analyzer\Bin;d:\Program\Intel\VTune\Shared\Bin;C:\Program Files\PC Connectivity Solution\;c:\program files\vni\ctt5.0\ctt5.0\lib\ia32;%systemroot%\system32;%systemroot%;%systemroot%\system32\wbem;c:\program files\ibm\trace facility;c:\program files\personal communications;c:\program files\ati technologies\ati control panel;c:\program files\common files\adaptec shared\system;c:\program files\ibm\trace facility\;c:\program files\intel\fortran\idb80\bin;%intel_fortran80%\ia32\bin;c:\program files\host integration server\system;c:\program files\ibm\personal communications\;c:\progra~1\ca\shared~1\scanen~1;c:\program files\ca\sharedcomponents\scanengine;c:\program files\ca\sharedcomponents\caupdate\;c:\program files\ca\sharedcomponents\thirdparty\;c:\program files\ca\sharedcomponents\subscriptionlicense\;c:\progra~1\ca\etrust~1;C:\Program Files\MATLAB\R2007a\bin;C:\Program Files\MATLAB\R2007a\bin\win32;C:\Program Files\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDE
VNI_DIR
C:\Program Files\VNI\CTT5.0\CTT5.0\..
VNI_F90_MSG
C:\Program Files\VNI\CTT5.0\CTT5.0\BIN\IA32
Intel Fortran 10.1 and VS. Net 2005
=====================================
Manually add this to SYSTEM VARIABLE -> Path from Control Panel
D:\Program\VNI\imsl\fnl600\IA32\LIB;
C:\Program files\MPICH2\bin;
D:\Program\Intel\Compiler\Fortran\10.1.013\Ia32\Bin;
C:\Program Files\Common Files\Intel\Shared Files\Ia32\Bin;
D:\Program Files\Microsoft Visual Studio 8\Common7\IDE;
D:\Program Files\Microsoft Visual Studio 8\VC\BIN;
D:\Program Files\Microsoft Visual Studio 8\Common7\Tools;
D:\Program Files\Microsoft Visual Studio 8\Common7\Tools\bin;
D:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\bin;
Manually add this to SYSTEM VARIABLE -> Lib from Control Panel
C:\Program files\MPICH2\LIB;%IFORT_COMPILER10%Ia32\Lib;%MSVS8%\VC\atlmfc\lib;%MSVS8%\VC\lib;%MSVS8%\VC\PlatformSDK\lib;%FNL_DIR%\IA32\lib;
Managed Code
=============
Mixed-Language Programming and Intel Visual Fortran Project Types
This version of Intel Visual Fortran produces only unmanaged code, which is architecture-specific
code. You cannot create an Intel Visual Fortran main program that directly calls a
subprogram implementing managed code. To call managed code, you can call an unmanaged
code subprogram in a different language that does support calling managed code.
BLAS, IMSL and MKL
===================
Blas is implemented by IMSL - details are found in Chapter 9: Basic Matrix/Vector Operations.
Blas is also implemented by the hardware vendor - in this case Intel - in Intel's MKL library,
which may be written in machine code.
The BLAS API, i.e. the calling convention of the routines, are the same whether they are
implemented by MKL or IMSL. For example, SDOT is the routine that finds the dot product of two
vectors.
To use different implementation, the program has to link with different libraries.
For IMSL: imslblas_dll.dll
For MKL: mkl_p4.dll
By default, when using link_f90_dll.h, it include's IMSL's BLAS (see section "Setup IMSL")
By default, when using link_f90_dll_smp.h, it include's MKL's BLAS (see section "Setup IMSL")
If we want to use MKL without the SMP (parallel processing) feature, then instead of using
link_f90_dll.h, we have to manually add the directives and point to the correct BLAS, eg:
!dec$objcomment lib:'imsl_dll.lib'
!dec$objcomment lib:'imslscalar_dll.lib'
!dec$objcomment lib:'mkl_ia32.lib'
The DLL (*.dll) can be placed anywhere the system knows of, eg:
c:\windows\system32\ mkl_def.dll, mkl_p3.dll, mkl_p4.dll
(IMSL provides these 3 dlls from the MKL package)
The mkl_ia32.lib contain STATIC INTERFACES to dlls including BLAS, cblas, FFTs, VML.
However, there is no corresponding single mkl_ia32.dll. Instead it is spread over a few DLLs,
such as mkl_def.dll, mkl_vml_def.dll, mkl_lapack32.dll, etc.
If a function (eg vsinv from VML package of MKL) is included in the library mkl_ia32.lib,
but the dll does not exist, then the code WILL COMPILE. But during runtime, a fatal error
would occur because it cannot find and use the dll.
NOTE: the IMSL dlls and libs are installed in
C:\Program Files\VNI\CTT5.0\CTT5.0\lib\IA32
Building DLLs (Fortran DLLs used in Fortran apps)
=================================================
Note:
When a DLL is built the output are two files:
1) *.dll - has the library's executable code
2) *.lib - the import library providing interface between the program and the dll.
The notes here presents two cases:
Case A: DLL to be created in its own separate VS solution, called solnA, in project projA.
The two generated output will be projA.dll and projA.lib
Case B: DLL to be created in a project (projB) in the same solution (solnB) , as the
application project (projC).
(The application project contains the code that uses the DLL.)
The two generated output will be projB.dll and projB.lib
1. Build DLL project in its own solution
- Say we call this Solution solnFoo, and Project projFoo
Case A:
- From VS.Net - in new solution, create a new DLL project by:
File -> New -> Project -> Intel Fortran projects -> Dynamic link library
Case B:
- From VS.Net - in existing solution, create a new DLL project by:
File -> New -> Project -> Intel Fortran projects -> Dynamic link library
2. Write a subroutine and expose it, eg:
subroutine hello()
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'_hello' :: hello
(do blah blah)
end subroutine hello
- put this subroutine by itself into a file (eg. hello.f90) or into
a module (eg hello_mod.f90)
- DLLEXPORT needed to expose the name of the routine
- alias is needed for compatibility with Intel Fortran and VS.NET environment
3. Build the DLL in VS.NET by:
- Build (menu) -> Build or Build Solution
- Copy the *.lib and *.dll files and put them into same directory as the
executable code for the application; i.e. same directory as projC.exe
4. Link the DLL via the lib file by:
- Go to the application project "program" file or "module" file and put this near the
start of the file:
CASE A: !dec$objcomment lib:'projA.lib'
CASE B: !dec$objcomment lib:'projB.lib'
CASE B only:
- Ensure that the dependencies eg projB is UNchecked in the Project Dependency dialog box of ProjC
- in the solution explorer in VS.NET, click on the application's
project name, eg projC.
- From the Project menu or right clicking on the project, go to "Add existing item ..."
- Browse and choose "projB.lib" to add. The lib file should appear under solution explorer.
- From the Project menu or right clicking on the project, go to "Project Dependencies..."
- Alternative to the "Add Existing item..." way is to specify through the linker by:
with the project name highlighted, go to Project menu -> Properties -> Linker
-> "Additional Library Directories" -> type in dir path where *.lib is located.
5. Add interface to DLL routine in the application.
- goto into the subroutine of the application and add the following:
module projC_app
contains
subroutine app()
interface
subroutine hello()
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'_hello' :: hello
end subroutine hello
end interface
end subroutine
end module
- DO NOT ADD the interface on the top level, eg DO NOT add in the starting part of a module. Instead
add the interface inside the module's subroutine that makes the call to the DLL routine.
- compile and run. Ensure that building mode is RELEASE, not DEBUG.
!DEC$ ATTRIBUTES directives
============================
1. C vs STDCALL - for controlling the stack of passed variables.
- both of these will try to make variables pass by value, rather than the Fortran default of
passing by reference.
- arrays are always passed by reference
- C -> the calling routine controls the stack. larger code.
- C -> possible to call variable number of arguments, MUST use "C, VARYING" to let
Fortran know that multiple arguments are called.
- C -> is default in C/C++ code. to use with fortran code, either
i) change the c code to STDCALL; or
extern void __stdcall foo_f90(int n);
ii) change the f90 code to use C convention
!DEC$ ATTRIBUTES C :: foo_f90
- STDCALL -> the called routine controls the stack.
2. VALUE vs REFERENCE
- for fortran, C or STDCALL will change default to passing by value, except arrays which will
be passed by reference
- But, each argument of the subroutine can be declared with VALUE or REFERENCE to override the
default mode, eg:
subroutine foo(a, b)
!DEC$ ATTRIBUTES VALUE :: a
!DEC$ ATTRIBUTES REFERENCE :: b
Passing module variables and functions in DLL
==============================================
Consider passing the variable 'foo' and calling method fooA() defined in a module 'mod_foo'
1. Expose the variable foo and fooA()
!DEC$ ATTRIBUTES DLLEXPORT :: foo
!DEC$ ATTRIBUTES DLLEXPORT :: fooA
Do not use ALIAS.
2. Build and Copy the following files from the DLL build directory to the application directory.
mod_foo.dll, mod_foo.lib, mod_foo.mod
3. In the application that uses 'foo', add the statement:
use mod_foo
This technique is only useful when both application and DLL are written in Fortran. The variable
names will have leading underscore "_". This is transparen to the user who uses "use mod_foo".
Such DLL are not convenient for DLLs that are to be used with other languages because of the leading
underscore on variable names.
Best Practice
==============
1. For optimized code:
- use /fast
- use "Configuration Properties -> Fortran -> Optimization -> Require Intel Processor Extension
2. To check for stack overflow
- /Qfpstkchk
- /Ge, /Gsn, /Fn
3. Fortran DLL structure
- Put constant data into a module, say mod_consts, and expose to data as:
!DEC$ ATTRIBUTES DLLEXPORT :: eps4
Note: do not ALIAS
- Put subroutines into another module, say mod_funcs and expose data:
use mod_consts
subroutine blah()
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'_testInt4' :: testInt4
Note: use alias so it is accessible outside
- Construct interface modules for application:
module interface_mod
use mod_consts
interface
subroutine blah()
!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'_testInt4' :: testInt4
............
- Include interface in the application
use interface_mod
This technique allows other Fortran projects to make use of both data and functions in DLLs.
However, other languages will not be able to make use of the data directly (may need to have underscore
for variable names in the other languages calling this Fortran DLL).
Errors - Debugging
===================
General Sources:
"List of Run-Time Error Messages", Intel Visual Fortran compiler doc
Cryptic LNK errors
1. When using a function from another place, eg DLL, etc; ensure that an "interface" block is written
for at the code which calls the function.
2. Ensure the library path is defined. Eg. In VS.Net -> right click project -> Properties -> Linker
-> General -> "Additional Library Directories"
Access Violation
1. Passing integer*4 into a subroutine with parameter declared as integer*8
2. Subroutine A in a module is DLL exported. Another subroutine within the same project uses subroutine A from another module WILL cause a CONFLICT. Since it is being used within the same project, subroutine A need a wrapper which is NOT DLL exported. This wrapper can be called by other module subroutines within the same project.
3. When an ARRAY of derived type contains components which are also derived types, then it must be declared with fixed size (i.e. hardcode dimension) or the variable must be a dynamic array (i.e. declared ALLOCATABLE). It cannot be declared with size specified by a parameter.
eg.
function foo(a, b)
real :: NestedDerivedTypeA(4) ! GOOD
real, allocatable :: NestedDerivedTypeB(:) ! GOOD
real :: NestedDerivedTypeA(b) ! BAD
Derived Data Type - Nested
1. Complicated derived data types that involves nested derived types will not be able to be displayed in the debuger / variable watch space. The displayed numbers are grossly in error.
DLL not exposed properly -
When calling a function in a dll, but that function has not been exposed, then the following error may occur:
"The procedure entry point ..... could not be located in the dynamic link library ....dll"
VSL/MKL errors
Message:
MKL ERROR : Parameter 2 was incorrect on entry to vslNewStre
Cause:
using MKL, VSL, VML routines from intel, and having directives like:
!dec$objcomment lib:'mkl_c_dll.lib'
!dec$objcomment lib:'mkl_ia32.lib'
are missing the path to the ...mkl\ia32\lib
Solution:
In VS.Net, within the dll/console project that uses them, add the path to the library files in:
Project -> Properties -> Linker -> General -> Additional Library Directories
IMSL Errors
Message:
Error: There is no matching specific subroutine for this generic subroutine call.
Cause:
IMSL documentation shows Fortran90 version with D_RNCHI, but unless using somehow, still obeying
Fortran77. So use Fortran77 name which is DRNCHI.
Solution:
Instead of using Fortran90 style -> D_RNCHI
we use -> DRNCHI
How to Add Version and other Metadata to DLL or EXE
=====================================================
Assume platform is Intel Fortran 8.1 and VS.Net 2003, but may apply to later versions too.
1. Go to Solutions Explorer and right click on the project name.
2. Choose Add New Item. In the Add New Item dialog, choose resource. A resourceX.rc will be created in the "Resource Files" folder directly under the project directory. Perhaps if this file already exist, we can skip to the next step.
3. Double click to open the resourceX.rc file.
4. In the resourceX.rc file, right click on the name resourceX.rc and choose "Add resource..."
5. In the "Add Resource" dialog, choose Version.
6. Fill in the relevant versioning and metadata info that is required.
7. Then build the project.
8. Check by right-clicking on the dll or exe file.
Using VTune
============
To use VTune, the following needs to be set up:
1. From VS.NET -> Project -> Properties -> Linker -> Debug -> Generate Program Database File
.... ensure this pdb file is defined.
From VS.NET -> Project -> Properties -> Fortran -> Debugging -> Debug Information Format
.... Full(/Zi)
2. Put this "/FIXED:NO" in:
VS.NET -> Project -> Properties -> Linker -> Command Line -> Additional Options
.... this is to ensure that VTune's Call Graph can be used. This only applies to the executable project.
3. Application to Launch - select and app or driver/dll that is already running.
Call Graph - must specify application to Launch.
Sampling and Counter may select "No App to launch"
4. Counter Monitor - Intel recommend using this first.
- uses native Windows performance counters, eg. processor queue, memory, processor time
- Has the following info:
- the Logged Data view
- the Legend
- the Summary view
- the Data Table - click on Logged Data View first to access
- Two main monitors to check are:
- %Processor Time: The closer to 100% the better. This is calculated by taking amount
of time spent in the Idle thread and subtracting from 100%
- System Processor Queue length - There is a single queue for processor time even on
multiprocessor systems. This counter should be less than 2. It measures how many
threads are waiting to execute.
- Intel Tuning Advice - to get the advice, from the Logged Data View, highlight the
section of the graph of interest. Then click on the Tuning Assistant button.
- Drill Down to Correlated Sampling Data View.
- To use sampling data, need to collect sampling data when collecting counter data.
Compiler Options
===================
/iface:[no]mixed_str_len_arg
Default: /iface:nomixed_str_len_arg
Specifies the type of argument-passing conventions used for general arguments and for hidden-length character arguments.
Possible values are:
/iface:mixed_str_len_arg: The hidden lengths should be placed immediately after their corresponding character argument in the argument list, which is the method used by Microsoft* Fortran PowerStation.
/iface:nomixed_str_len_arg: The hidden lengths should be placed in sequential order at the end of the argument list. When porting mixed-language programs that pass character arguments, either this option must be specified correctly or the order of hidden length arguments changed in the source code.
See also Programming with Mixed Languages Overview and related sections.
Compiling - Diagnostics.
To perform diagnostics such as using Vtune, Thread Profiler or Thread Checker, some of these options may be needed:
/Zi - include symbols
/Od - disable debugging
/fixed:no - linked to make code relocatable
/MDd - to build with thread safe libraries.
Build Macros (eg $(OUTDIR))
============================
See Intel Visual Fortran - User Guide - Volume I: - Building apps from MS Visual Studio.Net - Supported Build Macros.
Example:
In the project properties - Linker - Output File, the value is "$(OUTDIR)/xxx.dll".
The macro $(OUTDIR) has a value defined in:
project properties - Output Directory
Similarly $(INTDIR) is defined in
project properties - Intermediate Directory
Using MKL
===========
1. CBA desktop PC - Pentium 4 CPU 3.8GHz
- from intel website:
CPU No.: 670; 90 nm; Cache: 2 MB L2; Clock Speed: 3.80 GHz; FSB: 800 MHz
Hyperthreading, Enhanced SpeedStep, Intel64 (need Bios and OS), ExecuteBit Enabled
2. Installation Directories:
- c:\Program Files\intel\mkl\8.1.1
- tools\environment -> mklvarsXXX.bat to build environment variables.
- 3 options: ia32, em64t, ia64; within these are dlls and libs files
- for the ia32 option, ia32\bin contain:
mkl_lapack_YY.dll, mkl_XXX.dll, mkl_vml_XXX.dll, mkl_ias.dll, libguide40.dll
YY = 32,664
XXX = def, p3,p4, p4p, p4m
- for the ia32 option, ia32\lib contain:
mkl_X.lib, mkl_X_dll.lib, mkl_lapack.lib, mkl_solver.dll, mkl_ia32.lib, libguide40.lib, libguide.lib
X = c (for c), s (for Fortran)
3. Configuring to use MKL
- at installation time, say yes to add vars to PATH, LIB, INCLUDE.
- alternatively, run mklvars32.bat
4. Using Fortran95 BLAS or LAPACK
- Need to build from Intels sources, go to mkl\8.1.1\interfaces\blas95,lapack95
- nmake PLAT=win32 lib -> a *.mod file will be created
- or go to INCLUDE directory and: ifort -c mkl_lapack|blas.f90
- Or to make it in the user's directory:
1. copy mkl\8.1.1\interfaces\blas95,lapack95 into <user>
2. copy from INCLUDE to <user> these files: mkl_lapack|blas.f90
3. run in the blas,lapack directories: nmake PLAT=win32 INTERFACE=mkl_blas|lapack.f90 lib
for 64 bit
- nmake can be found in C:\Program Files\Microsoft Visual Studio 8\VC\bin\
- from the Start Menu, open Intel Visual Fortran Build Environment using Intel 64.
- nmake PLAT=win32e lib
- mod files will be automatically copied to ..../em64t
5. Linking to library:
a) see "Linking your application with Intel MKL" in "Getting Started with the Intel Math
Kernel Library 8.1.1 for Windows" for reference.
b) In VS.Net, go to Project menu -> Properties -> Linker -> General -> Additional Library Directories
and put:
C:\Program Files\Intel\MKL\8.1.1\ia32\lib
6. Errors
a) Compile error:
SortProj1 error LNK2019: unresolved external symbol _VSLNEWSTREAM referenced in function _MAIN__.L
Solution:
1. put the following in the code at start of module or program, NOT subroutine or function
use MKL_VSL_TYPE
use MKL_VSL
!dec$objcomment lib:'mkl_c_dll.lib'
!dec$objcomment lib:'mkl_ia32.lib'
2. Could also be sometimes need DLLIMPORT rather than DLLEXPORT, especially in RELEASE version????
3. If the function is a Fortran95 function, such as gemv, then the solution is to "call dgemv.." rather
than "call gemv..."
b) Runtime error:
MKL ERROR: Parameter 2 was incorrect on entry to vslNewStre
Solution:
In VS.Net, go to Project menu -> Properties -> Linker -> General -> Additional Library Directories
and put:
C:\Program Files\Intel\MKL\8.1.1\ia32\lib
7. Prerequisite Directories - these need to be put in Project -> Properties or command line or etc...
1. Include Directories: C:\Program Files\Intel\MKL\8.1.1\include
2. Library Directories: C:\Program Files\Intel\MKL\8.1.1\ia32\lib
3. Put the following line in the start of one of the source code, before the program or module keyword.
include 'mkl_vsl.fi' ! This is a full-fledged module by MKL
4. Put the following at the start of a module or program, not within a function or subroutine
use MKL_VSL_TYPE
use MKL_VSL
!dec$objcomment lib:'mkl_c_dll.lib'
!dec$objcomment lib:'mkl_ia32.lib' implicit none
Using LAPACK95 & General Comment on DLLs, LIBs, Mod files
==========================================================
To illustrate the usage of Lapack functions with Fortran95 interface,
suppose we want to use subroutine GESV
Fortran77 call: sgesv, dgesv, cgesv, zgesv
Fortran95 call: gesv
gesv is an Interface in mkl_lapack.f90(module MKL95_LAPACK)
gesv interface overloads wrappers like DGESV_MKL95, etc....
Only two items are needed by the user -> *.lib and *.mod
DLL
- not needed because we will be using explicit interfaces.
- Also F95 lapack routines have optional arguments which REQUIRE interfaces (eg gesv).
LIB
- mkl_lapack95.lib needed (created once off by administrator or first user)
- Use in the code as:
!dec$objcomment lib:'mkl_lapack95.lib'
!dec$objcomment lib:'mkl_c_dll.lib'
!dec$objcomment lib:'mkl_ia32.lib'
- Don't need
!dec$objcomment lib:'mkl_lapack.lib'
- must be linked during compile time either
i) ifort ..... mkl_lapack95.lib; or
ii) specify the path in "Additional Library Directories"
MOD
- mkl95_lapack.mod needed (created once off by administrator or first user from mkl_lapack.f90)
- contains the collection of interfaces to be used in the code by having:
USE MKL95_LAPACK
- must be present during compile time in the directory path of either:
i) same location as application source files.f90
ii) INCLUDE directories as specified in VS.Net as "Additional Include Directories"
Mixed language programming
============================
Hi Clinton,
It looks like library format incompatibility problem. We adhere to microsft format.
Please follow following steps as a work-around ;
Once you generate .dll from intel FORTRAN compiler; follow the following steps,
1. D:\>pedump /exp MatlabFunctions.dll > MatlabFunctions.exp
D:\>notepad MatlabFunctions.exp (Edit this file and replace MATEXP with _MATEXP)
D:\>buildlib MatlabFunctions.exp MatlabFunctions.lib
D:\> lcc hello.c
D:\>lcclnk hello.obj MatlabFunctions.lib
D:\>hello.exe
Stack Checking
===============
Checking and Setting Space
The following options perform checking and setting space for stacks (these options are supported on Windows only):
The /Gs0 option enables stack-checking for all functions.
The /Gsn option checks by default the stack space allocated for functions with more than 4KB.
The /Fn option sets the stack reserve amount for the program. The /Fn option passes /stack:n to the linker.
Enable Vectorization and Report
================================
To enable automatic vectorization, use these switches:
/Qx... or /Qax....
To enable report, use:
/Qvec-report....
Enable OpenMP
==============
1. To enable openMP;
by Command line: /Qopenmp /Qfpp
by VS.net: Project -> Properties -> Preprocessor -> OpenMP conditional compilation -> Yes
Project -> Properties -> Preprocessor -> Preprocess source file -> Yes (/fpp)
Project -> Properties -> Language -> Process OpenMP directives -> Generate Parallel code (/Qopenmp)
Note: preprocessor must be enabled for the OpenMP directives to be processed.
2. For diagnostic report:
by Command line: /Qopenmp-report
3. Compile OpenMP but in sequential mode;
by Command line: /Qopenmp-stubs
or to Compile for single thread, use the preprocessor /Qfpp, but not the OpenMP /Qopenmp.
4. DO NOT USE /Qprof-genx with OpenMP - spurious errors like array out of bounds will result.
5. To use OpenMP functions like, omp_get_num_threads(), instead of using
include "omp_lib.h",
better to use:
external omp_get_num_threads
integer omp_get_num_threads
Using Thread Profiler
=======================
1. Compiler options to enable Thread Profiling:
a) /Zi - full debugging format
b) /fixed:no - linker option to make code relocatable
c) /MDd - option tells the linker to search for unresolved references
in a multithreaded, debug, dynamic-link (DLL) run-time library.
This is the same as specifying options /libs:dll /threads /dbglibs.
d) /Qopenmp-profile - enable profiling of OpenMP.
WARNING: this option should not be used with IMSL since IMSL will link to libguide or libguide40, but
this option creates code that will link to libguide_stats or libguide40_stats
Profile Guided Optimization
============================
This is a 3 step process:
1. Compile with /Qprof-gen. Using /Qprof-genx allows Code Coverage tool to be used.
DO NOT USE /Qprof-genx WITH OPENMP.
2. Run the code one or many times with different data sets.
This will create .dyn files.
3. Compile with /Qprof-use. This uses the .dyn file created in step 2.
4. Usually specify /O2 in step 1, and more aggresive /Qipo in step 3.
5. Need the following:
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE;
C:\Program Files\Microsoft Visual Studio 8\VC\BIN;
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools;
C:\Program Files\Microsoft Visual Studio 8\Common7\Tools\bin;
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\bin;
msvcr80d.dll -> C:\Program Files\Microsoft Visual Studio 8\VC\redist\Debug_NonRedist\x86\Microsoft.VC80.DebugCRT
Intel Array Visualizer
========================
Array Visualizer is a free tool developed by Intel that plots graphs of arrays. The best usage for this is when it is available via the IDE (such as Visual Studio) such that when you are debugging Fortran code in the IDE and when you right click on an array variable, you have the option to view the array as a graph.
Installing Array Visualizer when you have the Intel Fortran already installed (including integration with Visual Studio) can be a bit tricky. You can install it but the "View Array" option is disabled. To install it properly here is the procedure:
1. Assume you have Visual Studio and Intel Fortran (and Integration with Visual Studio) already installed and working.
2. Then download Array Visualizer and install on the same PC.
3. Then run the installation for Intel Visual Fotran and step through the installation setup wizard until you can select the Modify option from {Modify, Repair, Remove} options.
4. Continue with the installation wizard and select integration with Microsoft Visual Studio option.
At the end of this, the Array Visualizer will be available within the IDE.
Tuesday, May 05, 2009
Monday, May 04, 2009
Notes SQL
Contents
=========
DB Termininology
Select Statement
Update Statement
Insert Statement
Delete Statement
Examples
Exporting DB
Replace Text / String
GROUP BY, DISTINCT
INNER JOIN ... ON
LEFT, RIGHT, FULL JOIN
DB tasks
COUNT
DB Termininology
=================
DDL Data Definition Language
CREATE
DROP
ALTER
TRUNCATE
DML Data Manipulation Language
SELECT
UPDATE
INSERT
DELETE
DCS Data Control Statements
GRANT
CONNECT
REVOKE
COMMIT
ROLLBACK
LOCK TABLE
AUDIT
Other Commands
desc
select count(*) from table - count the total records for the table.
SQLPlus
select * from cat - list all tables
Select Statement
=================
SELECT [columns] FROM [tables] WHERE [search_condition] LIKE [pattern] ORDER BY [order_expression ASC|DSC]
* col1='blah' 'B%'
col1,col2 col1 < '123' '%B'
cond1 AND cond2 '[abcd]%'
[Agg func]
... where
[Agg func] = aggregate function = Avg(), Sum(), Min(), Max(), Count(*), Count(DISTINCT col)
WHERE logical operators
= < > >= <= <> NOT !=
BETWEEN foo AND bar
IN
The IN operator implements comparison to a list of values, that is, it tests whether a value matches any value in a list of values. IN comparisons have the following general format:
value-1 [NOT] IN ( value-2 [, value-3] ... )
This comparison tests if value-1 matches value-2 or matches value-3, and so on. It is equivalent to the following logical predicate:
value-1 = value-2 [ OR value-1 = value-3 ] ...
or if NOT is included:
NOT (value-1 = value-2 [ OR value-1 = value-3 ] ...)
For example,
SELECT name FROM s WHERE city IN ('Rome','Paris')
Update Statement
=================
UPDATE [table] SET [set_expression] WHERE [search_condition]
Insert Statement
==================
INSERT INTO [table] ([column list]) VALUES ([value list])
(col1, col2) ('one', 'two', 3)
INSERT INTO [table] ([column list]) SELECT "column3", "column4", ... FROM "table2"
INSERT INTO [table] ([column list])
SELECT "column3", "column4", ...
FROM ( MERGE ................. OUTPUT ....... )
AS [dummy table name] ([column list])
Delete Statement
=================
DELETE FROM [table] WHERE [search condition]
Examples
=========
select menuid from menu where foodid not in (select foodid from food);
--- check if there are any rows in menu with a foodid that is not in the food table. Note that
menuid and foodid are the primary keys for the menu and food table respectively.
Compare values in one table but not the other.
select distinct B.ex from B
where B.ex not in (select distinct F.ex from F)
Exporting DB
=============
When exporting a DB or table within a DB, we can use the Export function in HeidiSQL.
Note: Need to remove "MySQL specific comments" such as "/*«number» ... */" when uploading
to phpAdmin.
In the example below, need to remove "/*!32312 IF NOT EXISTS*/" or remove "/*!32312 */"
CREATE TABLE /*!32312 IF NOT EXISTS*/ `stateau` (
`StateID` int(10) unsigned NOT NULL auto_increment,
`Name` varchar(50) default NULL,
`Code` varchar(3) default NULL,
`Postcode` mediumint(8) unsigned default NULL,
PRIMARY KEY (`StateID`)
) TYPE=InnoDB AUTO_INCREMENT=4 /*!40100 DEFAULT CHARSET=latin1*/;
In MySQL syntax, the number in /*«number» ... */ stands for the version number.
Replace Text / String
======================
UPDATE Products_Descriptions SET ProductDescription = REPLACE(LTRIM(RTRIM(ProductDescription)), '©', '©')
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
GROUP BY, DISTINCT
====================
O_Id OrderDate OrderPrice Customer
1 2008/11/12 1000 Hansen
2 2008/10/23 1600 Nilsen
3 2008/09/02 700 Hansen
4 2008/09/03 300 Hansen
5 2008/08/30 2000 Jensen
6 2008/10/04 100 Nilsen
Now we want to find the total sum (total order) of each customer.
We will have to use the GROUP BY statement to group the customers.
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
The result-set will look like this:
Customer SUM(OrderPrice)
Hansen 2000
Nilsen 1700
Jensen 2000
Using GROUP BY by more than one column. When grouping, all columns that appear in the SELECT column list, that are not aggregated (used along with one of the SQL aggregate functions), have to appear in the GROUP BY clause too. For example:
SELECT CustomerName, OrderDate, SUM(OrderPrice) FROM Sales
GROUP BY CustomerName, OrderDate
DISTINCT cannot be used to get the results above.
Note that the following statements are the same:
SELECT DISTINCT(Customers), OrderPrice FROM Orders
SELECT DISTINCT Customers, OrderPrice FROM Orders
SELECT Customers, OrderPrice FROM Orders
Their results are:
1000 Hansen
1600 Nilsen
700 Hansen
300 Hansen
2000 Jensen
100 Nilsen
Distinct always applies across all column names listed in the SELECT statement.
INNER JOIN ... ON
===================
The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
The "Orders" table:
O_Id OrderNo P_Id
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 15
Now we want to list all the persons with any orders.
We use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
ORDER BY Persons.LastName
The result-set will look like this:
LastName FirstName OrderNo
Hansen Ola 22456
Hansen Ola 24562
Pettersen Kari 77895
Pettersen Kari 44678
LEFT, RIGHT, FULL JOIN
========================
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (table_name2).
SQL LEFT JOIN Syntax
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases LEFT JOIN is called LEFT OUTER JOIN.
The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if there are no matches in the left table (table_name1).
SQL RIGHT JOIN Syntax
SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
PS: In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
The FULL JOIN keyword return rows when there is a match in one of the tables.
SQL FULL JOIN Syntax
SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
DB tasks
==========
Copy table from one DB to another DB, given that the structure of the table has been created in the
destination DB.
(MSSQL)
insert into DB2.dbo.table1
select * from DB1.dbo.table1
List all column names in a table
(MSSQL)
SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'TableName'
SELECT Column_Name + ',' FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = 'TableName'
SELECT table_name=sysobjects.name,
column_name=syscolumns.name,
datatype=systypes.name,
length=syscolumns.length
FROM sysobjects
JOIN syscolumns ON sysobjects.id = syscolumns.id
JOIN systypes ON syscolumns.xtype=systypes.xtype
WHERE sysobjects.xtype='U'
ORDER BY sysobjects.name,syscolumns.colid
Select First N rows
(MSSQL)
SELECT TOP 10 name, description FROM TABLEA ORDER BY name
The above query will return the first 10 rows sorted by name. How do I get the bottom 10 rows? Use the DESC keyword in the ORDER BY clause and it will sort it in reverse order.
Counting the number of categories
select count(*) , ENTITY_ID
from Merge_CBA_ASBCommProp
group by ENTITY_ID
The results are:
(name) ENTITY_ID
957240 3
830842 1
492520 2
COUNT
========
If the following code returns 15 records:
SELECT distinct a, b
FROM Tab
then to count the number for rows returned, we need:
SELECT count(*)
FROM
(
SELECT distinct a, b
FROM Tab
) AS outt
Note "AS outt" is needed for the inner results table to be ALIASED, otherwise the outer select cannot use it.
Labels:
Full join,
Inner join,
insert,
replace text,
select statement,
sql server 2008,
update
Notes ASP do tNet
NotesASPdotNet
Contents
=========
Tutorial
Structure
ASP.Net Configuration
Connecting Events
HTML Control Class
Web / Server Control Class
ASP postback - eg like AJAX
ADO.Net - Accessing Data the Easy Way
ADO.Net - Example
ADO.Net - command parameters
ADO.Net - Data Binding
ADO.Net - Data Binding - DataList
ADO.Net - Styles and Templates summary
Windows Service Applications
VS Setup Projects
Understanding ADO.Net - from AppDev video tutorial
Using MySQL in Visual Studio
Configuring MySQL with ASP.Net
DropDownList
DataGrid - Sorting
WebService in ASP.Net
ASP.Net Custom Control
Tutorial
=========
1. Visual Studio 2005 -> Help -> Contents -> .Net Development
-> Web Applications -> ASP.Net Quickstart Tutorials
Structure
===========
System.Web.UI - Generic Page Class
|
V
custom page class (my *.cs file, eg HelloClass.cs)
|
V
custom aspx file (my *.aspx file, eg Hello.aspx)
|
V
Page object
System.Web.HttpApplication
|
V
class GlobalApp (global.cs)
|
V
global.asax -> contain event handling code, each application can have one global.asax.
The classes are linked together like this:
VS2005: Global.asax no longer created automatically but can be added by:
Add new item -> Global Application Class.
Also the code behind global.asax.cs is no longer created, instead put the code inline in global.asax
Hello.aspx
-----------
<%@ Page Language="CS" Inherits="HelloClass" Src="HelloClass.cs" %> - if source is supplied
<%@ Page Language="CS" Inherits="HelloClass" %> - if dll is supplied
<html>
<body>
<form id="Form" runat="server">
<asp:Label id="lblTest" runat="server" />
</form>
....
HelloClass.cs
--------------
public class HelloClass : System.Web.UI.Page {
protected System.Web.UI.WebControls.Label lblTest;
private void Page_Load(){
lblTest.Text = "Hello World";
}
}
some basic event available to global.asax inherited from HttpApplication:
Application_OnStart
Application_OnEnd
Application_OnBeginRequest
Application_OnEndRequest
Application_OnError
Session_OnStart
Session_OnEnd
ASP.Net Configuration
======================
1. machine.config - one per server, located at:
Microsoft.Net/Framework/Version/Config/
2. web.config - one per application, as well as one in each virtual sub-directory.
3. Structure and contents of web.config
+++++++++++++
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!-- custome settings - using your own keyword -->
<appSettings>
<add key="ConnectionString" value="Data Source=localhost;Initial Catalog=Pubs;User ID=sa"/>
<add key="SelectSales" value="Select * FROM Sales"/>
</appSettings>
<system.web>
<httpRuntime />
<pages />
<compilation />
<customErrors />
<authentication />
<authorization />
<identity />
<trace />
<sessionState />
<httpHandlers />
<httpModules />
<globalization />
</system.web>
</configuration>
+++++++++++++
4. To make use of the special custom settings, eg.
++++++++++
using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Project
{
public partial class ShowCustomConfigSettings : System.Web.UI.Page
{
protected Label lblTest;
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = "This app will connect with the connection string <br><br>";
lblTest.Text += ConfigurationSettings.AppSettings["ConnectionString"];
lblTest.Text += "<br><br> and will execute the SQL statement <br>";
lblTest.Text += ConfigurationSettings.AppSettings["SelectSales"];
}
}
}
++++++++++
Connecting Events
===================
case: A button called id="Convert" on the aspx page. Functionality is in the function called
"Convert_ServerClick"
On the aspx page, have the attribute in the <%@Page .... >
AutoEventWireup="false"
To connect the function "Convert_ServerClick" to the event:
1. protected override void OnInit(EventArgs e) {
InitializeComponent();
base.OnInit(e);
}
2. private void InitializeComponent() {
Convert.ServerClick += new EventHandler(Convert_ServerClick);
}
Note that EventHandler is a DELEGATE that connects a the function "Convert_ServerClick"
The EventHandler delegate would have the same prototype of the function it points to, ie.:
protected void Convert_ServerClick(Object sender, EventArgs e)
The ServerClick would be a type of the EventHandler delegate; perhaps
public event EventHandler ServerClick
perhaps defined under the Convert's class - HtmlInputButton
HTML Control Class
=======================
System.Object
System.Web.UI.Controls
HtmlControl ---> System.Web.UI.HtmlControls namespace
HtmlImage
HtmlInputControl
HtmlInputButton
HtmlInputCheckBox
HtmlInputFile
HtmlInputHidden
HtmlInputImage
HtmlInputRadioButton
HtmlInputText
HtmlContainerControl
HtmlAnchor
HtmlButton
HtmlForm
HtmlGenericControl
HtmlSelect
HtmlTable
HtmlTableCell
HtmlTableRow
HtmlTextArea
Web / Server Control Class
============================
System.Object
System.Web.UI.Controls
Repeater ---> System.Web.UI.WebControls
WebControl
AdRotator
Calendar
ValidationSummary
BaseDataList (abstract)
DataGrid
DataList
ListControl (abstract)
CheckBoxList
DropDownList
ListBox
RadioButtonList
Button
CheckBox
RadioButton
Hyperlink
Image
ImageButton
Label
BaseValidator (abstract)
CompareValidator
CustomValidator
RangeValidator
RegularExpressionValidator
RequiredFieldValidator
LinkButton
Panel
Table
TableCell
TableHeaderCell
TableRow
TextBox
ASP postback - eg like AJAX
============================
1. Web control events that can use POSTBACK, when AutoPostBack property is set
to true:
Events Web Control
======= ===========================
Click Button, ImageButton
TextChange TextBox
CheckChanged CheckBox, RadioButton
SelectedIndexChange DropDownList, ListBox, CheckBoxList, RadioButtonList
2. ASP.Net automatically adds these to the HTML page:
<input type="hidden" name="__EVENTTARGET" value=""/>
<input type="hidden" name="__EVENTARGUMENT" value=""/>
<script language="javascript">
<!--
function __doPostBack(eventTarget, eventArgument){
var theform = document.Form1;
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
-->
3. Add event handlers to the InitializeComponent() method
i.e. <button>.<Event> += new EventHandler(<functionName>)
eg txt.TextChanged += new EventHandler(CtrlChanged)
4. Implement using the added function:
private void CtrlChanged(Object sender, EventArgs e) { }
ADO.Net - Accessing Data the Easy Way
=============================
To retrieve information
1. Create Connection, Command and DataReader objects
2. Use DataReader to retrieve info and display it on web
3. Close connection.
4. Send page to user.
To add or update information
1. Create new Connection and Command objects.
2. Execute the Command
ADO.Net - Example
=========
using System.Data;
using System.Data.SqlClient; // for MS SQL
using System.Data.OleDb; // for general connector
// Define the connection to Database
<x>={Sql, OleDb, ...}
<x>Connection myConn = new <x>Connection();
myConn.ConnectionString = "Provider=SQLOLEDB.1; Data Source=localhost;" + " Initial Catalog=Pubs; User ID=root";
// the provider is need when <x>=OleDb, but not when <x>=Sql.
// Password is not passed here if using "Integrated Windows Authentication"
myConn.ConnectionString = "Provider=SQLOLEDB.1; Data Source=localhost;" + " Initial Catalog=Pubs; Integrated Security=SSPI";
// Making the connection
try{
myConn.Open();
lblInfo.Text="<b>Server Version: </b>" + myConn.ServerVersion;
lblInfo.Text+="<br><b> Connection is:</b> " + myConn.State.ToString();
} catch(Exception err) {
lblInfo.Text = "Error reading the database.";
lblInfo.Text += err.Message;
} finally {
myConn.Close();
lblInfo.Text += "<br> Now Connection is";
lblInfo.Text += myConn.State.ToString();
}
//Alternative connection
using{
myConn.Open();
lblInfo.Text="<b>Server Version: </b>" + myConn.ServerVersion;
lblInfo.Text+="<br><b> Connection is:</b> " + myConn.State.ToString();
}
lblInfo.Text += "<br> Now Connection is";
lblInfo.Text += myConn.State.ToString();
// Creating the SQL statement and assigning to Command
<x>Command myCmd = new <x>Command();
myCmd.Connection = myConn;
myCmd.CommandText = "Select * from Authors";
//Alternative
<x>Command myCmd = new <x>Command("Select * from Authors", myConn);
// Using Command with DataReader
<x>DataReader myReader;
myReader = myCmd.ExecuteReader();
// Reading objects
myReader.Read(); // reads an object at a time sequentially
// Cleanup
myReader.Close();
myConn.Close();
ADO.Net - command parameters
==============================
Instead of using this:
insertSQL "Insert INTO authors (au_id) VALUES ('txtID.Text')"
.... where txtID is a from the GUI on the webpage.
This method can be hacked via SQL Injection. To solve this, Command Parameters are used:
insertSQL "Insert INTO authors (au_id) VALUES (?)"
cmd.Parameters.Add("?", txtID.Text);
where cmd is a OleDBCommand, and note that "?" is for OleDB only.
MySQL or MSSQL may have different symbols or notation for their command parameters.
ADO.Net - Data Binding
=======================
Simple Data Binding:
1) in the aspx page, have something like:
<asp:Label id="lbLabel" runat="server">
There were <%# TransactionCount %>
</asp:Label>
2) In the code behind, have something like:
private void Page_Load(...){
TransactionCount = 10;
this.DataBind();
Alternative to data bind above is to do this in-code.
3) private void Page_Load(...){
TransactionCount = 10;
lblDynamic.Text = "There were " + TransactionCount.ToString();
Multiple Binding:
1. Form an array list from whereever:
eg. ArrayList fruit = new ArrayList();
fruit.Add("Kiwi");
or from database
2. Define binding for list controls; eg
MyListBox.DataSource = fruit;
MyDropDownListBox.DataSource = fruit;
MyHTMLSelect.DataSource = fruit;
MyCheckBoxList.DataSource = fruit;
MyRadioButtonList.DataSource = fruit;
3. Activate the binding, eg.
this.DataBind(); // this refers to the current page.
Multiple Binding with hashtables:
1. Create hashtable with key,val pair
Hashtable fruit = new Hashtable();
fruit.Add(1, "Kiwi");
fruit.Add(2, "Pear");
2. Binding specific fields
MyListBox.DataTextField = "Value" -> put the Value of hastable into the Text of the Control
MyListBox.DataValueField = "Key" -> put the Key of hastable into the value of the Control
3. Define databinding and activate
MyListBox.DataSource = fruit;
4. The result is the following HTML will be rendered:
<select name="MyListBox" id="MyListBox">
<option value="1">Kiwi</option>
Databinding with databases:
1. Some possible namespaces (when using OleDB):
using System.Data;
using System.Data.OleDb;
2. Making data using dataset example:
DataSet ds = new DataSet();
ds.Tables.Add("Users");
ds.Tables["Users"].Columns.Add("Name");
ds.Tables["Users"].Columns.Add("Country");
DataRow dr = ds.Tables["Users"].NewRow();
dr["Name"] = "John";
dr["Country"] = "Uganda";
ds.Tables["Users"].Rows.Add(rd);
3. Bind table to data source:
lstUser.DataSource = ds.Tables["Users"];
lstUser.DataTextField = "Name";
or Bind whole DataSet to data source:
lstUser.DataSource = ds;
lstUser.DataMember = "Users";
lstUser.DataTextField = "Name";
4. Activate the Binding:
this.DataBind();
or to bind just the list box
lstItems.DataBind()
ADO.Net - Data Binding - DataList
=====================================
1. Code Behind:
public class BasicAuthorList: Page
{
protected DataList listAuthor;
// (Initialisation code omitted)
private string connectString ="Provider ......."
private void Page_Load(Object sender, EventArgs e)
{
string SQL = "SELECT * FROM AUTHORS";
OleDbConnection con = new OleDbConnection (connectString);
OleDbCommand cmd = new OleDbCommand(SQL, con);
OleDbAdapter adapt = new OleDbAdapter(cmd);
DataSet pubs = new DataSet();
con.Open();
adapter.Fill(pubs, "Authors");
con.Close();
//Bind the DataSet and activate the data bindings for the page
listAuthor.DataSource = pubs.Tables["Authors"];
this.DataBind();
2. ASPX DataList template
<asp:DataList id=listAuthor runat="server">
<ItemTemplate>
<font face="Verdana" size="2">
<b><%# DataBinder.Eval(Container.DataItem, "au_fname") %>
<%# DataBinder.Eval(Container.DataItem, "au_lname") %></b></font>
<br> Address: <%# DataBinder.Eval(Container.DataItem, "address") %>
<br> City: <%# DataBinder.Eval(Container.DataItem, "city") %>
</font>
</ItemTemplate>
</asp:DataList>
3. Formating Values - for DataBinder
eg DataBinder.Eval(Container.DataItem, "Price", "{0:C}")
{0:C} Currency
{0:E} Scientific (Exponential)
{0:P} Percentage
{0:F?} Fixed Decimal
see MSDN Help for more.
4. Adding different styles to Templates
a) Manual option - hand coding on aspx file
<HeaderTemplate>
<h2> title </h2>
</HeaderTemplate>
<ItemTemplate>
(Item style1)
</ItemTemplate>
<AlternatingItemTemplate>
(Item style2)
</AlternatingItemTemplate>
<SeparatorTemplate>
<h2> title </h2>
</SeparatorTemplate>
<FooterTemplate>
<h2> title </h2>
</FooterTemplate>
b) Using VS.Net right-click DataList properties
c) AutoFormat link from the DataList properties
d) To make columns and rows, check out the RepeatDirection and RepeatColumns properties.
ADO.Net - Data Binding - DataGrid
=====================================
1) DataList may have columns and rows, but DataGrid has columns such that the columns
correspond to certain fields of the data item; eg phone - city - zip code as different
columns.
2) Features include: automatic paging, sorting, editing, selecting.
3) Examples that show the Columns tag and the use of styles:
<asp: DataGrid id=gridauthor runat="server" AutoGenerateColumns="false"
BorderColor="#..." BorderStyle="None" CellSpacing="2"
BackColor="#..." CellPadding="3" BorderWidth="1px">
<FooterStyle ForeColor="#..." BackColor="#..." ></FooterStyle>
<HeaderStyle ForeColor="#..." BackColor="#..." ></HeaderStyle>
<ItemStyle ForeColor="#..." BackColor="#..." ></ItemStyle>
<Columns>
<asp:TemplateColumn HeaderText="AuthorName">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "au_fname") %>
<%# DataBinder.Eval(Container.DataItem, "au_lname") %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
4) DataGrid has special columns including:
TemplateColumn - contents here include <ItemTemplate> and other stuff that can go into
DataList.
BoundColumn - a particular field from database
ButtonColumn
EditCommandColumn
HyperlinkColumn
All these columns should exist within <Columns> ... </Columns>
ADO.Net - Data Binding - Repeater
=====================================
Repeater has not much formatting by itself. It depends on formatting provided
in the html. Example:
<asp:Repeater id=repeatauthor runat="server">
<HeaderTemplate> <table> </HeaderStyle>
<ItemTemplate><tr>
<%# DataBinder.Eval(Container.DataItem, "au_fname") %>
</ttr></ItemTemplate>
<FooterTemplate> </table> </FooterStyle>
</asp:DataGrid>
ADO.Net - Styles and Templates summary
=======================================
Styles
DataList DataGrid Repeater
-----------------------------------------------------------
AlternatingItemStyle AlternatingItemStyle None
EditItemStyle EditItemStyle
FooterStyle FooterStyle
HeaderStyle HeaderStyle
ItemStyle ItemStyle
SelectedItemStyle SelectedItemStyle
SeparatorStyle PagerStyle
Template
DataList DataGrid* Repeater
-----------------------------------------------------------
AlternatingItemTemplate FooterTemplate AlternatingItemTemplate
EditItemTemplate HeaderTemplate FooterTemplate
FooterTemplate ItemTemplate ItemTemplate
HeaderTemplate EditItemTemplate SeparatorTemplate
ItemTemplate
SelectedItemTemplate
SeparatorTemplate
* only supported by Template column
1) Do not bind the grid in the Page.Load event handler, otherwise info is
lost about which button user clicked or item selected.
private void Page_Load......
if(!this.PostBack) {
DataSet ds = GetDataSet();
BindGrid(ds);
.........
}
private DataSet GetDataSet(){
string SQL = "...."
OldDbConnection ...
OldDbCommand cmd =
OleDbAdapter = ...
DataSet dsPubs = ...
adapter.Fill(dsPubs, "Authors");
con.Close();
return dsPubs;
}
private void BindGrid(DataSet ds){
gridAuthor.DataSource = ds.Table["Authors"];
this.DataBind();
}
Windows Service Applications
=============================
1. Do not have any in/output to screen or Windows, or it will crash the app.
2. Error messages should be logged in the Windows event log rather than raised in the user interface.
3. To control, use Services Control Manager, or Server Explorer, or the ServiceController class (use this
to control the service from another app).
4. States of the service include: start, stop, pause, resume
Corresponding methods in code: OnStart, OnStop, OnPause, OnContinue, OnShutdown, OnCustomCommand, OnPowerEvent.
5. Two types of services: Win32OwnProcess, Win32ShareProcess.
6. VisualStudio has installation components that can install resources, register the service and let Services Controller
Manager know. Add these installers to the app and also create a separate setup project.
7. Must inherit from System.ServiceProcess.ServiceBase class. The project must contain installation components.
To create the Service:
- set ServiceName property
- create installers
- override methods for OnStart and OnStop.
8. System.ServiceProcess.ServiceProcessInstaller and System.ServiceProcess.ServiceInstaller
?You use these classes to install and uninstall your service.
9. Execution Process:
Add Installers in project
Build
Install: installutil yourproject.exe / Uninstall: installutil /u yourproject.exe
Start the Service
10. Debugging:
Install service
Start service
In VS, Debug->Process
Click Show System Processes
Attach process
add any break points
use Services Control Manager to control the service.
11. To add Installers:
-In Solution Explorer, go to Design View of the service.
-Click on background of designer and AddInstaller.
-A new class, ProjectInstaller, and two installation components, ServiceProcessInstaller and ServiceInstaller,
are added to your project, and property values for the service are copied to the components.
-Click the ServiceInstaller component and verify that the value of the ServiceName property is set to the
same value as the ServiceName property on the service itself.
-To determine how your service will be started, click the ServiceInstaller component and set the StartType property
to the appropriate value. { Manual, Automatic, Disabled }
- To determine the security context in which your service will run, click the ServiceProcessInstaller
component and set the appropriate property values. For more information, see How to: Specify the Security Context for Services.
- Override any methods for which you need to perform custom processing. For more information, see How to: Override Default Methods on Installation Components.
- Perform steps 1 through 7 for each additional service in your project.
Note: For each additional service in your project, you must add an additional ServiceInstaller component
to the project's ProjectInstaller class. The ServiceProcessInstaller component added in step
three works with all of the individual service installers in the project.
- Create your setup project and custom action to deploy and install your service.
For more information on setup projects, see Setup Projects.
- After you add installers to your application, the next step is to create a setup project that will install
the compiled project files and run the installers needed to install your service. To create a complete
setup project, you must add the service project's output to the setup project and
then add a custom action to have your service installed. For more information on setup projects, see Setup Projects. For more information on custom actions, see Walkthrough: Creating a Custom Action.
(see VS Setup Projects for more details)
VS Setup Projects
=====================
1. Design, code, build your project in VS Studio say project ProjA in solution SolnA.
2. Create the setup project SetupA, in SolnA: Add Project -> New Project -> Other Project Types -> Setup and Deployment.
3. Right click setupA -> Install -> Add Project Output. Select ProjA and choose "Primary Output".
4. Right click setupA -> View -> Custom Actions.
5. Right click on Custom Actions -> Add Custom Option. Select Application Folder -> Primary output from ProjA.
6. Build.
7. The msi and exe file will be created.
Understanding ADO.Net - from AppDev video tutorial
====================================================
Object Model:
Connected Object (Specific to DB engine or provider)
-> .Net Data Provider (incl. MySql.Data.MySqlClient or SystemData.ODBC|OleDB|SqlClient|OracleClient)
-> Connection: Transaction
-> Command: Parameters (eg to execute some command)
-> DataReader:
-> DataAdapter:
-> Fills DataTable: SelectComm
-> Updates Database: InsertComm, UpdateComm, DeleteComm
DisConnected Object (can work with data in a disconnected state)
-> DataSet (analogous to Database) <-> XML
-> DataTableCollection
-> DataTable:
-> DataRowCollection - storing the data
-> DataColumnCollection - names and properties of columns
-> ConstraintCollection - eg unique key or foreign key constraints.
-> DataRelationCollection
-> DataView
-> Row Filter
-> Sort
-> Extended support for Data Binnding
-> DataTableReader
-> A DataReader for DataTables and DataSets
-> Fast forward only reading
-> resilient to adding or deleting rows while reading
... to be continued at Data Caching Object
Using MySQL in Visual Studio
=============================
1. View -> Server Explorer
2. Look for database server and connect
Configuring MySQL with ASP.Net
===============================
It is easy to use MySQL with ASP.Net. becasue by default the local ASP.Net enables you with FULL TRUST permission.
This section shows how to configure ASP.Net to work with MySQL where the ASP.Net is stored in a ISP's server,
which usually uses MEDIUM TRUST, not FULL TRUST.
To the SQL administrator:
1. There are two options, one is to modify the web_mediumtrust.config; the other option is to copy and rename the config file.
The web_mediumtrust.config is located typically in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\
Add the SOCKPermission to this file.
<SecurityClass .............
<SecurityClass Name="SocketPermission" Description="System.Net.SocketPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
...
...
<IPermission
class="SocketPermission"
version="1"
Unrestricted="true"/>
2. If we choose the second option in step 1. to make a new file and called web_mediumMySQLtrust.config, then we need
to register this in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config with the following line:
<trustLevel name="Medium" policyFile="web_mediumtrust.config" /> .... already there
<trustLevel name="MediumMySQL" policyFile="web_mediumMySQLtrust.config" /> .... add this new one
Also in the same file, add this within the <system.web>
<trust level="Medium" originUrl="" />
or
<trust level="MediumMySQL" originUrl="" />
To the Developer:
1. In the web application, in the web.config file, insert the line about <trust> just before </system.web>
eg.
<system.web>
......
......
<trust level="Medium" originUrl="" />
</system.web>
DropDownList
============
To add first element, add a ListItem and the property AppendDataBoundItems set to true.
Eg.
<asp:dropdownlist id="suburbDropList" AppendDataBoundItems="true" AutoPostBack="True" runat="server" >
<asp:ListItem Text="(Select a Suburb)" Value="" />
</asp:dropdownlist>
DataGrid - Sorting
===================
To allow for sorting on the DataGrid, the following are needed.
1. Right click on the GUI design of the DataGrid and set "AllowSorting" to "true".
2. Add the following in the aspx code file in the DataGrid item:
OnPageIndexChanging="PageChangeFunction" OnSorting="SortingFunction"
3. Set property AutoGenerateColumns to true, i.e.TestMenu.AutoGenerateColumns = true;
WebService in ASP.Net
======================
*.asmx - web service file
WSDL - Web Service Description Language, describes the methods and parameters in a web service.
The elements of WSDL are: definitions, types, message, portType, binding, service.
SOAP - way to encode info to pass to web service
HTTP - protocol through which SOAP messages are sent
DISCO - discovery standard that contains links to web services
UDDI - business registry that lists all info about companies and web services they provide, with
URLs for their WSDL contracts and DISCO.
Data types: Basic / primitives, Enumerations, DataSets, XmlNode, Custom Objects, Arrays
Testing the web service
------------------------
http://<domain>/<webservice>.asmx -> to view the web service directly
http://<domain>/<webservice>.asmx?WSDL -> to view the WSDL.
http://<domain>/<webservice>.asmx?op=<methodName> -> to access the web service method directly
The use of web services in C# ASP.Net is illustrated with the example below:
----------------------------------
1.<%@ WebService Language="C#" Class="Util" %>
2. using System.Web.Services;
3. using System;
4. [WebService(Namespace="http://www.contoso.com/")]
5. public class Util: WebService
6. {
7. [ WebMethod]
8. public long Multiply(int a, int b)
9. {
10. return a * b;
11. }
12. }
----------------------------------
or
----------------------------------
in file: Util.asmx
1.«%@ WebService Language="C#" CodeBehind="Util.asmx.cs" Class="Util" %»
in file: Util.asmx.cs
2. using System.Web.Services;
3. using System;
4. [WebService(Namespace="http://www.contoso.com/", Description="test utility")]
5. public class Util: WebService
6. {
7. [WebMethod(Description=" test a method "]
8. public long Multiply(int a, int b)
9. {
10. return a * b;
11. }
12. }
----------------------------------
1. Declare the webservice using «%@ WebService....
Option 1: Place the above code inside *.asmx file with the C# code.
Option 2: Put only the <%@ WebService in the asmx class and put the C# code somewhere else.
Then the declaration would be:
<%@ WebService Language="C#" Class="MyName.MyWebService,MyAssembly" %>
... where MyAssembly is the name of the assembly.
2. Deriving the WebService class - this is optional. Lines 2,5.
3. Apply WebService attribute and specify domain to avoid confusion with other webservices.
see line 4.
4. Define the web service method using [ WebMethod ] - see line 7.
5. State Management for Web Services is achived by deriving from the WebService class. It provides
access to ASP.NET objects such as:
Session - client specific state information
Application - used to store data globally and available to all clients
Server - utility functions
User - info about current client including authentication
Context - provides access to Request, Response and Cache
7. Storing and access state in a client session:
Declare: [ WebMethod(EnableSession=true) ]
Store: Session["MyServiceUsage"] = 1;
Access: Session["MyServiceUsage"] = ((int) Session["MyServiceUsage"]) + 1;
8. Storing and access state in Web application hosting the Web service:
Declare: [ WebMethod ]
Store: Application["appMyServiceUsage"] = 1;
Access: Application["appMyServiceUsage"] = ((int) Application["appMyServiceUsage"]) + 1;
ASP.Net Custom Control
=======================
The following describes Custom Control for web applications (*.aspx). The Custom Control for console
applications are similar, but not exactly.
1. To add a user control, right click on the web application project in the Solution Explorer. Click
New Item, then choose Web User Control, and name it WUC.
2. In Visual Studio, this will auto create 3 files:
- WUC.ascx - with Design and Code view
- WUC.ascx.cs - the code to be edited or added.
- WUC.ascx.designer.cs
3. Put the collection of controls in your custom control withing the WUC.ascx file. For example,
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WUC.ascx.cs"
Inherits="ParentWebApp.WUC" %>
<asp:Label ID="lblFooter" runat="server" Height="87px" Text="Label" Width="214px"></asp:Label>
4. The Label control above can be also created by using the WUC.ascx design view. Just
drag the Label button from the toolbox onto the form.
5. In the code behind file WUC.ascx.cs, make the WUC class inherit from System.Web.UI.UserControl. And
in the Page_Load, enter code for what the controls need to do. For example,
public partial class WebUserControlFooter : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
lblFooter.Text = "This page was served at ";
lblFooter.Text += DateTime.Now.ToString();
}
}
6. To use this custom control on any aspx web page:
a) Register the custom control on those aspx web pages, eg.
<%@ Register TagPrefix="cr" TagName="WUC" Src="~/WUC.ascx" %>
b) Add the custom control, eg.
<cr:WUC id=Footer1 runat="server" />
Note that the TagPrefix name is the alias for the custom control to be used in the aspx web page.
Saturday, May 02, 2009
How to connect Set Top Box
Connecting Set-Top box to TV and VHS/DVD Player
================================================
There are 3 hardware that need to be connected here.
i) TV set (analog, non-digital)
ii) Set top box (for receiving digital TV stations onto non-digital TV)
iii) VHS Recorder / DVD player combo.
Requirements - what we want:
i) To be able to view non-digital TV stations
ii) To be able to view digital tv stations
iii) To be able to use VHS recorder player to record both digital and non-digital stations.
iv) To be able to play DVD.
I am writing here to share my experience after I bought the set top box. I already had the TV and the VHS/DVD unit before. Basically, on top of the old functionalities, I wanted to view Digital TV as well as record it after I bought the set top box.
The connections are very simple once I figured it out - but initially, the instructions did not make much sense. Here is the KEY - the TV as well as the VHS/DVD units have multiple inputs for video signal known as AV1, AV2, etc. Once I understood this, everything became easy.
Connection TYpe: We will be using the 3 plug (RCA) Composite cables.
Red and White are for audio, Yellow is for the Video signal.
TV aerial:
i) Connect Wall Antenna to set top box (IN) with one TV cable.
ii) Connect the set top box (OUT) to the VHS/DVD unit (IN) with a second TV cable.
This is like chaining the antenna from wall to set top box to video recorder.
Set top box to VHS/DVD: connect this together via the composite cable (red-white-yellow plugs) from the output of the set top box to AV1 (input) of the VHS/DVD unit.
VHS/DVD to TV: Use a second set of composite cable to connect from the VHS/DVD unit (OUT) to an input on the TV set (eg another AV1).
What's Happening? Essentially the analog TV signal first goes to the set top box which converts to analog signal. From the set top box, the raw analog TV signal (analog TV channels) is re-transferred to the VHS player. The composite cable from set top box to VHS unit carries the digital TV channels.
Since the VHS/DVD player now has both analog and digital tv stations, the key to access both is that:
i) analog channels are accessed by switching the channels on the VHS/DVD unit.
ii) digiatal channels require the VHS/DVD unit to use AV1 or AV2 depending on which port you plug the 3 cables to the unit.
iii) the VHS can record both analog and digital channels.
VHS/DVD to TV: This requires one connection only using the 3plug composite cable. The signal from the VHS/DVD unit (OUT) to the TV (IN AV1, or IN AV2) can carry both analog and digital channels. So both analog and digital channels are available. Note that the AV1 or AV2 here belongs to the TV, and is different to the AV1 of the VHS/DVD unit. We must select AV1, or AV2, in order to get the signals from the VHS/DVD player.
Operating the Remote Controls
==============================
There are 3 remote controls, let's call them:
RC-STB for Set Top Box
RC-DVD for DVD
RC-TV for analog TV
The operation described works for the specific connections that I have described above. If you choose different connections, eg using AV2 instead of AV1, then you have to operate the Remote Control accordingly.
1. Switch on TV, DVD/VHS and Set Top Box in any order
2. Use RC-TV to select the TV's AV1 - never change this again.
3. For viewing or recording Digital TV signals (analog and digital from Set Top Box),
- use RC-DVD and select VHS in the VHS/DVD option buttons
- use RC-DVD and select A1 (stands for AV1)
- use RC-STB to flip through the digital channels and/or record to VHS tape.
4. For viewing or recording Analog TV signals (raw analog signal)
- use RC-DVD and flip through 1,2,3, etc..... and view and/or record to VHS tape.
- no need to use RC-STB at all here.
================================================
There are 3 hardware that need to be connected here.
i) TV set (analog, non-digital)
ii) Set top box (for receiving digital TV stations onto non-digital TV)
iii) VHS Recorder / DVD player combo.
Requirements - what we want:
i) To be able to view non-digital TV stations
ii) To be able to view digital tv stations
iii) To be able to use VHS recorder player to record both digital and non-digital stations.
iv) To be able to play DVD.
I am writing here to share my experience after I bought the set top box. I already had the TV and the VHS/DVD unit before. Basically, on top of the old functionalities, I wanted to view Digital TV as well as record it after I bought the set top box.
The connections are very simple once I figured it out - but initially, the instructions did not make much sense. Here is the KEY - the TV as well as the VHS/DVD units have multiple inputs for video signal known as AV1, AV2, etc. Once I understood this, everything became easy.
Connection TYpe: We will be using the 3 plug (RCA) Composite cables.
Red and White are for audio, Yellow is for the Video signal.
TV aerial:
i) Connect Wall Antenna to set top box (IN) with one TV cable.
ii) Connect the set top box (OUT) to the VHS/DVD unit (IN) with a second TV cable.
This is like chaining the antenna from wall to set top box to video recorder.
Set top box to VHS/DVD: connect this together via the composite cable (red-white-yellow plugs) from the output of the set top box to AV1 (input) of the VHS/DVD unit.
VHS/DVD to TV: Use a second set of composite cable to connect from the VHS/DVD unit (OUT) to an input on the TV set (eg another AV1).
What's Happening? Essentially the analog TV signal first goes to the set top box which converts to analog signal. From the set top box, the raw analog TV signal (analog TV channels) is re-transferred to the VHS player. The composite cable from set top box to VHS unit carries the digital TV channels.
Since the VHS/DVD player now has both analog and digital tv stations, the key to access both is that:
i) analog channels are accessed by switching the channels on the VHS/DVD unit.
ii) digiatal channels require the VHS/DVD unit to use AV1 or AV2 depending on which port you plug the 3 cables to the unit.
iii) the VHS can record both analog and digital channels.
VHS/DVD to TV: This requires one connection only using the 3plug composite cable. The signal from the VHS/DVD unit (OUT) to the TV (IN AV1, or IN AV2) can carry both analog and digital channels. So both analog and digital channels are available. Note that the AV1 or AV2 here belongs to the TV, and is different to the AV1 of the VHS/DVD unit. We must select AV1, or AV2, in order to get the signals from the VHS/DVD player.
Operating the Remote Controls
==============================
There are 3 remote controls, let's call them:
RC-STB for Set Top Box
RC-DVD for DVD
RC-TV for analog TV
The operation described works for the specific connections that I have described above. If you choose different connections, eg using AV2 instead of AV1, then you have to operate the Remote Control accordingly.
1. Switch on TV, DVD/VHS and Set Top Box in any order
2. Use RC-TV to select the TV's AV1 - never change this again.
3. For viewing or recording Digital TV signals (analog and digital from Set Top Box),
- use RC-DVD and select VHS in the VHS/DVD option buttons
- use RC-DVD and select A1 (stands for AV1)
- use RC-STB to flip through the digital channels and/or record to VHS tape.
4. For viewing or recording Analog TV signals (raw analog signal)
- use RC-DVD and flip through 1,2,3, etc..... and view and/or record to VHS tape.
- no need to use RC-STB at all here.
Labels:
Composite cable,
digital tv,
dvd recorder,
RCA,
tv,
vhs,
video
Monday, January 05, 2009
How to use Brook+ for GPU computing
AMD Stream Computing allows developers to use the GPU to perform parallel computations for HPC applications. This guide is meant to complement the AMD Stream Computing User Guide. It is essential to read the official User Guide to gain a brief understanding before following the notes below.
Ref: http://ati.amd.com/technology/streamcomputing/Stream_Computing_User_Guide.pdf
System - The following notes are compiled based on the following system.
Intel CPU
ATI Radeon (Check cards for GPU computing capability)
Microsoft Visual Studio .Net with C/C++ compilers - for C/C++ code
Intel Visual Fortran Compilers - for Fortran code
Brook+ SDK by AMD - to compile Brook code
br source file
================
The Brook+ source file contain code that follow C/C++ syntax and is compiled/pre-processed by the Brook+ compiler into C/C++ file. Both Brook+ functions and C/C++ functions can exist within the same *.br file. The Brook+ functions are the functions that utilises the GPU hardware.
Example of a Brook+ function is given below:
kernel void sumaa(float a<>, float b<>, out float c<>){
c = a + b;
}
1. Special Brook+ keywords (ie. Not C/C++ words): kernel, out
2. Note the template like structures "float a<>" which are recognized by the Brook+ compiler. They indicate stream / GPU data type and are not the same as C++ templates.
3. Multiple functions like the above can exist in the same *.br file. Other normal C/C++ functions can also exist inside the *.br file.
Compiling Brook+ Code (*.br)
=============================
0. Open up a Command Console and go to the directory where the *.br file is located.
1. To compile code called sum.br:
\sdk\bin\brcc_d -k sum.br
where is the installation directory of the Brook SDK from AMD.
2. This the brook+ compiler / preprocessor creates the following in the same directory.
sum.cpp
sum.h
sum_gpu.h
3. A few notes to consider
i) There are two compilers: brcc and brcc_d. They correspond to brook.lib/dll and brook_d.lib/dll respectively.
Using the wrong combination may crash the program during execution.
ii) The -k option generates intermediate code that may be useful for use with the AMD's Stream Kernel Analyzer.
iii) The C/C++ code that are generated need to be compiled using standard C/C++ compilers and link to the proper libraries and dlls, hence the next section.
iv) Before v1.3, C/C++ wrapper functions, also known as host side code, exist within the *.br source file. As of v1.3, the host side code can be written in C++ and exist in a separate normal C++ file, provided it is configured with the proper include and lib directory information.
Compiling the C/C++ code
==========================
This step produces a win32 DLL from the C/C++ code that are generated by Brook+. The resultant DLL should be
able to be used by other win32 applications (eg C++ or Fortran).
1. From Visual Studio .Net, Open a new solution / project by:
Add Project -> Visual C++ -> Win32 -> Win32 project.
In the Application Settings dialog, select DLL, Export Symbols
2. Add the *.br and the files generated by the Brook+ compiler into the current project by using
"Add existing file".
3. Under the Project Property configuration pages, add the following settings:
C++ -> Additional Include Directories:\sdk\include
C++ -> Code Generation -> Runtime Library: Multi-threaded Debug DLL (/MDd)
C++ -> Advanced -> Calling Convention: __cdecl (/Gd)
Linker -> Additional Library Directories:\sdk\lib
Linker -> Input -> Additional Dependencies:\sdk\lib\brook_d.lib
4. When the *.br is modified, compile the *.br files in Command Console, then compile the generated c/c++ code from within the VisualStudio.Net environment.
Some Notes:
i) One can configure VisualStudio.Net to accept *.br files and compile using the Brook+ compiler. However, I find
that it still requires the user to manually initiate compilation for Brook files and then for C/C++ files. Hence,
I don't find it to be any efficient than compiling by command line.
ii) The *.br source files can be added to the project and can be edited using the VS.Net environment.
The C/C++ driver or library wrapper
====================================
The Brook+ functions need to be wrapped or called directly from C/C++ functions. For the purpose of creating DLL functions, we will put C/C++ wrappers over the Brook+ functions.
The usage of the Brook+ functions involve 3 steps. Each of these step are described with examples here:
Declaring and sizing variables - the meaning and reason for the declarations will become clear in the following sections.
// Normal C/C++ variables
float input_a[10][10];
float input_b[10][10];
float input_c[10][10];
float input_a1[10];
float input_b1[10];
float input_c1[10];
// For dimensioning Brook+ variables
unsigned int ileng = 10;
unsigned int dims[2] = {10,10};
unsigned int dim1[1] = {10};
// Equivalent Brook+ variables
brook::Stream a(2, dims);
brook::Stream b(2, dims);
brook::Stream a1(1, dim1);
brook::Stream b1(1, dim1);
brook::Stream *c = new brook::Stream(1, &ileng);
brook::Stream *d = new brook::Stream(2, dims);
brook::Stream d1(1, dim1);
// Assign values to normal C/C++ vectors and matrices for:
// input_a1, input_b1, input_a, input_b
..................
1. Reading normal C/C++ variables into Brook+ variables
a.read(input_a);
b.read(input_b);
a1.read(input_a1);
b1.read(input_b1);
This step transforms a normal C/C++ variable into a Brook+ variable which the GPU can understand. No other manipulation need to be done to the Brook+ variable.
2. Performing the computation by calling the Brook+ function
sumaa(a,b,*d); // operating on a matrix
sumaa(a1,b1,d1); // operating on a vector
3. Writing the output from Brook+ into normal C/C++ variables
// old method
streamWrite(*d, input_c);
streamWrite(d1, input_c1);
// new method
d->write(input_c);
d1.write(input_c1);
Once the Brook+ variable has been copied back to a normal C/C++ variable, one can perform other standard operations to the normal C/C++ variable as desired.
Note the use of pointer d* and non-pointer d1 is just to show that both ways are possible.
Using with Fortran
====================
Brook+, being like an extension to C/C++, is better called from C/C++ functions. But, provided that C/C++ wrappers are built for the Brook+ functions and then packaged into a DLL library, then any other language, eg Fortran, can use the GPU by calling on the C/C++ wrappers in the DLL.
Brook+ functions <--- C/C+ wrappers <--- Windows DLL / Unix shared objects <--- Fortran
Ref: http://ati.amd.com/technology/streamcomputing/Stream_Computing_User_Guide.pdf
System - The following notes are compiled based on the following system.
Intel CPU
ATI Radeon (Check cards for GPU computing capability)
Microsoft Visual Studio .Net with C/C++ compilers - for C/C++ code
Intel Visual Fortran Compilers - for Fortran code
Brook+ SDK by AMD - to compile Brook code
br source file
================
The Brook+ source file contain code that follow C/C++ syntax and is compiled/pre-processed by the Brook+ compiler into C/C++ file. Both Brook+ functions and C/C++ functions can exist within the same *.br file. The Brook+ functions are the functions that utilises the GPU hardware.
Example of a Brook+ function is given below:
kernel void sumaa(float a<>, float b<>, out float c<>){
c = a + b;
}
1. Special Brook+ keywords (ie. Not C/C++ words): kernel, out
2. Note the template like structures "float a<>" which are recognized by the Brook+ compiler. They indicate stream / GPU data type and are not the same as C++ templates.
3. Multiple functions like the above can exist in the same *.br file. Other normal C/C++ functions can also exist inside the *.br file.
Compiling Brook+ Code (*.br)
=============================
0. Open up a Command Console and go to the directory where the *.br file is located.
1. To compile code called sum.br:
where
2. This the brook+ compiler / preprocessor creates the following in the same directory.
sum.cpp
sum.h
sum_gpu.h
3. A few notes to consider
i) There are two compilers: brcc and brcc_d. They correspond to brook.lib/dll and brook_d.lib/dll respectively.
Using the wrong combination may crash the program during execution.
ii) The -k option generates intermediate code that may be useful for use with the AMD's Stream Kernel Analyzer.
iii) The C/C++ code that are generated need to be compiled using standard C/C++ compilers and link to the proper libraries and dlls, hence the next section.
iv) Before v1.3, C/C++ wrapper functions, also known as host side code, exist within the *.br source file. As of v1.3, the host side code can be written in C++ and exist in a separate normal C++ file, provided it is configured with the proper include and lib directory information.
Compiling the C/C++ code
==========================
This step produces a win32 DLL from the C/C++ code that are generated by Brook+. The resultant DLL should be
able to be used by other win32 applications (eg C++ or Fortran).
1. From Visual Studio .Net, Open a new solution / project by:
Add Project -> Visual C++ -> Win32 -> Win32 project.
In the Application Settings dialog, select DLL, Export Symbols
2. Add the *.br and the files generated by the Brook+ compiler into the current project by using
"Add existing file".
3. Under the Project Property configuration pages, add the following settings:
C++ -> Additional Include Directories:
C++ -> Code Generation -> Runtime Library: Multi-threaded Debug DLL (/MDd)
C++ -> Advanced -> Calling Convention: __cdecl (/Gd)
Linker -> Additional Library Directories:
Linker -> Input -> Additional Dependencies:
4. When the *.br is modified, compile the *.br files in Command Console, then compile the generated c/c++ code from within the VisualStudio.Net environment.
Some Notes:
i) One can configure VisualStudio.Net to accept *.br files and compile using the Brook+ compiler. However, I find
that it still requires the user to manually initiate compilation for Brook files and then for C/C++ files. Hence,
I don't find it to be any efficient than compiling by command line.
ii) The *.br source files can be added to the project and can be edited using the VS.Net environment.
The C/C++ driver or library wrapper
====================================
The Brook+ functions need to be wrapped or called directly from C/C++ functions. For the purpose of creating DLL functions, we will put C/C++ wrappers over the Brook+ functions.
The usage of the Brook+ functions involve 3 steps. Each of these step are described with examples here:
Declaring and sizing variables - the meaning and reason for the declarations will become clear in the following sections.
// Normal C/C++ variables
float input_a[10][10];
float input_b[10][10];
float input_c[10][10];
float input_a1[10];
float input_b1[10];
float input_c1[10];
// For dimensioning Brook+ variables
unsigned int ileng = 10;
unsigned int dims[2] = {10,10};
unsigned int dim1[1] = {10};
// Equivalent Brook+ variables
brook::Stream
brook::Stream
brook::Stream
brook::Stream
brook::Stream
brook::Stream
brook::Stream
// Assign values to normal C/C++ vectors and matrices for:
// input_a1, input_b1, input_a, input_b
..................
1. Reading normal C/C++ variables into Brook+ variables
a.read(input_a);
b.read(input_b);
a1.read(input_a1);
b1.read(input_b1);
This step transforms a normal C/C++ variable into a Brook+ variable which the GPU can understand. No other manipulation need to be done to the Brook+ variable.
2. Performing the computation by calling the Brook+ function
sumaa(a,b,*d); // operating on a matrix
sumaa(a1,b1,d1); // operating on a vector
3. Writing the output from Brook+ into normal C/C++ variables
// old method
streamWrite(*d, input_c);
streamWrite(d1, input_c1);
// new method
d->write(input_c);
d1.write(input_c1);
Once the Brook+ variable has been copied back to a normal C/C++ variable, one can perform other standard operations to the normal C/C++ variable as desired.
Note the use of pointer d* and non-pointer d1 is just to show that both ways are possible.
Using with Fortran
====================
Brook+, being like an extension to C/C++, is better called from C/C++ functions. But, provided that C/C++ wrappers are built for the Brook+ functions and then packaged into a DLL library, then any other language, eg Fortran, can use the GPU by calling on the C/C++ wrappers in the DLL.
Brook+ functions <--- C/C+ wrappers <--- Windows DLL / Unix shared objects <--- Fortran
Labels:
AMD stream Computing,
ATI,
Brook,
C++,
Fortran,
GPU,
HPC,
multicore,
parallel programming,
Radeon,
SIMD
Wednesday, December 24, 2008
Using VB JoinView class in C# web application
JoinView is a class developed by Microsoft to address a feature that is lacking in ADO.Net, up to .Net framework 3.5. The main functionality is to provide something like a DataView for when two or more tables are joined via DataRelations. For more details, and the VB.Net source code, see
HOW TO: Implement a Custom DataView Class in Visual Basic .NET
http://support.microsoft.com/kb/325682
Using the VB JoinView class in a C# application is explained nicely at in the article:
Data From Multiple Tables in a DataGridView
http://www.onteorasoftware.net/post/Data-From-Multiple-Tables-in-a-DataGridView.aspx
However the article above does not explain how to actually make the VB code into a usable product, ie. a dll, so that it can be used in C#.
This blog explains how to construct the VB JoinView class into a dll and how to link it to a C# application.
1. Download the source file JoinView.vb from the first link above. The actual file to be downloaded is called JoinView.exe.
2. Assuming you are working in a C# project called Cproj, inside the solution called Soln. Then in the Soln solution, add a new VB project called JoinViewProj.
3. Manually copy the JOinView.vb file into the location of JoinViewProj.
4. In VisualStudio, use "Add Existing Item" to add the JoinView.vb file into the JoinViewProj project.
5. Build the JoinViewProj project.
6. Go to the Cproj project and add reference. Browse and locate the file JoinViewProj\bin\Debug\JoinView.dll and add this as the reference.
7. In the C# code add this statement: "using JoinViewProj;"
8. Now we are ready to use the JoinView class in our C# code. For example: "JoinView jv;"
The actual usage of the JoinView class is as follows:
ds.Relations.Add("CustOrd", ds.Tables["Cust"].Columns["CustomerID"], ds.Tables["Ord"].Columns["CustomerID"]);
ds.Relations.Add("EmpOrd", ds.Tables["Emp"].Columns["EmployeeID"], ds.Tables["Ord"].Columns["EmployeeID"]);
JoinView jv;
jv = new JoinView(ds.Tables["Ord"],
"OrderID,CustomerID,EmployeeID,OrderDate,CustOrd.CompanyName Company,CustOrd.ContactName Contact,CustOrd.ContactTitle Position,EmpOrd.FirstName,EmpOrd.LastName",
"OrderID='312'", "CustomerID DESC");
First argument: This appears to be the common child table in the two DataRelations.
Second argument: This is the names of the columns of the tables directly, such as "OrderID" which belongs to the "Ord" table. Other names like "CustOrd.CompanyName Company" comes from the Data Relation "CustOrd" and field "CompanyName"; and the name "Company" is an alias that will appear in the grid view.
Third argument: Filter the rows. In the example, only OrderID being 312 are selected. Note it appears taht the field, eg OrderID, must belong to the specified table, in this case, the "ord" table.
Fourth argument: Sort the rows in ASC or DESC order. In the example, it is sorted by CustomerID in a descending way. Note it appears the field, eg CustomerID, must belong to the specified table, in this case, the "ord" table.
HOW TO: Implement a Custom DataView Class in Visual Basic .NET
http://support.microsoft.com/kb/325682
Using the VB JoinView class in a C# application is explained nicely at in the article:
Data From Multiple Tables in a DataGridView
http://www.onteorasoftware.net/post/Data-From-Multiple-Tables-in-a-DataGridView.aspx
However the article above does not explain how to actually make the VB code into a usable product, ie. a dll, so that it can be used in C#.
This blog explains how to construct the VB JoinView class into a dll and how to link it to a C# application.
1. Download the source file JoinView.vb from the first link above. The actual file to be downloaded is called JoinView.exe.
2. Assuming you are working in a C# project called Cproj, inside the solution called Soln. Then in the Soln solution, add a new VB project called JoinViewProj.
3. Manually copy the JOinView.vb file into the location of JoinViewProj.
4. In VisualStudio, use "Add Existing Item" to add the JoinView.vb file into the JoinViewProj project.
5. Build the JoinViewProj project.
6. Go to the Cproj project and add reference. Browse and locate the file JoinViewProj\bin\Debug\JoinView.dll and add this as the reference.
7. In the C# code add this statement: "using JoinViewProj;"
8. Now we are ready to use the JoinView class in our C# code. For example: "JoinView jv;"
The actual usage of the JoinView class is as follows:
ds.Relations.Add("CustOrd", ds.Tables["Cust"].Columns["CustomerID"], ds.Tables["Ord"].Columns["CustomerID"]);
ds.Relations.Add("EmpOrd", ds.Tables["Emp"].Columns["EmployeeID"], ds.Tables["Ord"].Columns["EmployeeID"]);
JoinView jv;
jv = new JoinView(ds.Tables["Ord"],
"OrderID,CustomerID,EmployeeID,OrderDate,CustOrd.CompanyName Company,CustOrd.ContactName Contact,CustOrd.ContactTitle Position,EmpOrd.FirstName,EmpOrd.LastName",
"OrderID='312'", "CustomerID DESC");
First argument: This appears to be the common child table in the two DataRelations.
Second argument: This is the names of the columns of the tables directly, such as "OrderID" which belongs to the "Ord" table. Other names like "CustOrd.CompanyName Company" comes from the Data Relation "CustOrd" and field "CompanyName"; and the name "Company" is an alias that will appear in the grid view.
Third argument: Filter the rows. In the example, only OrderID being 312 are selected. Note it appears taht the field, eg OrderID, must belong to the specified table, in this case, the "ord" table.
Fourth argument: Sort the rows in ASC or DESC order. In the example, it is sorted by CustomerID in a descending way. Note it appears the field, eg CustomerID, must belong to the specified table, in this case, the "ord" table.
Wednesday, December 03, 2008
Online Scanning websites and links for virus, malware, spyware
The content of this site has been moved to Online Scan - Websites
It contains links to online scanning tools to scan websites to check if websites are infected with trojans or malware.
It contains links to online scanning tools to scan websites to check if websites are infected with trojans or malware.
Labels:
google,
malware,
scandoo,
security holes,
siteadvisor,
website check
Friday, August 22, 2008
Technical Links
Programming - Memory
Understanding Virtual Memory - http://www.ualberta.ca/CNS/RESEARCH/LinuxClusters/mem.html
Programming - Tips
How to Write Unmaintainable Code - Ensure a Job for life ;)
http://thc.org/root/phun/unmaintain.html
The hidden power of Google Earth
http://www.pcpro.co.uk/features/145593/the-hidden-power-of-google-earth.html
from PC Authority Mar 2008
Includes the following topics:
Peel back the layers
Make your own virtual tours
Share photos with the world
Reach for the stars
Take to the skies
Model your house in 3D
How Google Earth works
Boost your broadband speed for free
http://www.pcauthority.com.au/Feature/119238,boost-your-broadband-speed-for-free.aspx
from PC Authority Aug 2008
Web's Best 50 Free Downloads
Stack your system full of software without paying a penny, with our guide to essential downloads
from PC Authority June 2008
Understanding Virtual Memory - http://www.ualberta.ca/CNS/RESEARCH/LinuxClusters/mem.html
Programming - Tips
How to Write Unmaintainable Code - Ensure a Job for life ;)
http://thc.org/root/phun/unmaintain.html
The hidden power of Google Earth
http://www.pcpro.co.uk/features/145593/the-hidden-power-of-google-earth.html
from PC Authority Mar 2008
Includes the following topics:
Peel back the layers
Make your own virtual tours
Share photos with the world
Reach for the stars
Take to the skies
Model your house in 3D
How Google Earth works
Boost your broadband speed for free
http://www.pcauthority.com.au/Feature/119238,boost-your-broadband-speed-for-free.aspx
from PC Authority Aug 2008
Web's Best 50 Free Downloads
Stack your system full of software without paying a penny, with our guide to essential downloads
from PC Authority June 2008
Wednesday, July 23, 2008
Callback to C# from Unmanaged Fortran
Using unmanaged code eg Fortran to callback to C# can be a nightmare to get right. But once you know it, it is just following a recipe. Hence without further explanation, an example and recipe is given below. It is quite self-expalnatory I hope.
Just note that, the example is more than a simple call back. The C# actually calls a Fortran function in a dll. Within the Fortran function calls the callback in C#.
--- Fortran code -----
module f90Callback
contains
subroutine func1(iArr, progressCllBak)
!DEC$ ATTRIBUTES DLLEXPORT ::func1
!DEC$ ATTRIBUTES REFERENCE :: iArr, progressCllBak
implicit none
external progressCllBak
integer :: iCB
integer, INTENT(OUT) :: iArr(2)
! Variables
! Body of f90Callback
print *, "Hello Before"
iCB = 3
iArr(1) = 5
iArr(2) = 7
call progressCllBak(iCB)
print *, "setting callback value in Fortran as :", iCB
print *, "Hello After"
return
end subroutine func1
end module f90Callback
------- C# code ---------
Just note that, the example is more than a simple call back. The C# actually calls a Fortran function in a dll. Within the Fortran function calls the callback in C#.
--- Fortran code -----
module f90Callback
contains
subroutine func1(iArr, progressCllBak)
!DEC$ ATTRIBUTES DLLEXPORT ::func1
!DEC$ ATTRIBUTES REFERENCE :: iArr, progressCllBak
implicit none
external progressCllBak
integer :: iCB
integer, INTENT(OUT) :: iArr(2)
! Variables
! Body of f90Callback
print *, "Hello Before"
iCB = 3
iArr(1) = 5
iArr(2) = 7
call progressCllBak(iCB)
print *, "setting callback value in Fortran as :", iCB
print *, "Hello After"
return
end subroutine func1
end module f90Callback
------- C# code ---------
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace CScallbackDriver
{
class Program
{
// 0. Define a counter for the Progress callback to update
public int localCounter;
// 1. Define delegate type
[UnmanagedFunctionPointer(CallingConvention=CallingConvention.Cdecl)]
public delegate void dgateInt(ref int numYears);
// 2. Create a delegate variable
public dgateInt dg_progCB;
public Program() {
// 3. Instantiate delegate, typically in a Constructor of the class
dg_progCB = new dgateInt(onUpdateProgress);
}
// 4. Define the c# callback function
public void onUpdateProgress(ref int progCount) {
localCounter = progCount;
}
int iArg;
static void Main(string[] args)
{
Program myProg = new Program();
myProg.localCounter = 0;
int[] iArrB = new int[2];
//6. Call normal Fortran function from DLL, and passing the callback delegate
func1(ref iArrB[0], myProg.dg_progCB);
Console.WriteLine("Retrieve callback value as {0}", myProg.localCounter);
Console.ReadKey();
}
// 5. Define the dll interface
[DllImport("f90Callback", EntryPoint="F90CALLBACK_mp_FUNC1", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern void func1([In, Out] ref int iArr, [MarshalAs(UnmanagedType.FunctionPtr)] dgateInt blah);
}
}
Labels:
c#,
callback,
delegate,
Fortran90,
function pointer,
unmanaged code
Sunday, July 13, 2008
Online Scan - AntiVirus
As with Firewalls, there should be at most, one Antivirus program running on your PC. More than that will cause uncertain behaviour. But how do we know that our antivirus is a good one. Or if we scan a file and it passed our antivirus scan, are we sure it is OK? One solution is to scan the file or files using online scanners available from the web. Here are a few of them:
http://housecall.trendmicro.com/
http://www.kaspersky.com/virusscanner
http://www.bitfender.com/scan8/ie.html
http://www.pandasecurity.com/homeusers/solutions/activescan
http://us.mcafee.com/root/mfs
http://onecare.live.com/site/en-US/default.htm
http://pestpatrol.com/
http://www.f-secure.com/en/web/labs_global/removal/online-scanner
Even easier is to upload one file and have it scanned by multiple antivirus software. This can be done at: http://www.virustotal.com/
Malware can be checked by submitted a file to this website:
http://malwr.com/about/
Alternatively, one can use a sandbox to test our suspicious applications first. Some of the sandbox are:
Norman Sandbox - www.norman.com/microsites/nsic
(this article can also be found at:
http://pckingsford.com//index.php?option=com_content&task=blogcategory&id=19&Itemid=39 )
http://housecall.trendmicro.com/
http://www.kaspersky.com/virusscanner
http://www.bitfender.com/scan8/ie.html
http://www.pandasecurity.com/homeusers/solutions/activescan
http://us.mcafee.com/root/mfs
http://onecare.live.com/site/en-US/default.htm
http://pestpatrol.com/
http://www.f-secure.com/en/web/labs_global/removal/online-scanner
Even easier is to upload one file and have it scanned by multiple antivirus software. This can be done at: http://www.virustotal.com/
Malware can be checked by submitted a file to this website:
http://malwr.com/about/
Alternatively, one can use a sandbox to test our suspicious applications first. Some of the sandbox are:
Norman Sandbox - www.norman.com/microsites/nsic
(this article can also be found at:
http://pckingsford.com//index.php?option=com_content&task=blogcategory&id=19&Itemid=39 )
Labels:
antivirus,
bitdefender,
f-secure,
kaspersky,
onecare,
online scan,
protection,
security
Saturday, July 12, 2008
Firewall Testing (Hardening)
Here are some tools and techniques to test if your firewall is up to scratch:
1. www.pcflank.com
Contains various test for the firewall including: port scanners, stealth testing, leak test and others.
For leak test, download PCFlankLeaktest.exe. This test tries to send some information out of your PC to the PCFlank site. The results are shown in http://www.pcflank.com/pcflankleaktest_results.htm
. According to PCFlank, only Outpost Firewall Pro and Tiny Personal Firewall pass the leaktest. At PCKingsford, we have tried the FREE Comodo Firewall Pro (CFP) and this works. If it fails, you just need to be sure that you have not tell CFP to ALLOW it. To check whether an application is allowed in CFP, open up the CFP, go to the Defense+ on top, then go to the Advanced tab on the left menu, then click Computer Security Policy. Look for the application name and edit the rules for it.
2. ShieldsUp at http://www.grc.com/faq-shieldsup.htm
Click on Services tab on the website, then select ShieldsUp. Follow the instruction to test your firewall via this website. The tests include:
File Sharing
Common Ports
All Service Ports
Messenger Spam
Browser Helpers
3. Leaktest 1.2 at http://www.grc.com/lt/leaktest.htm This is one of the original firewall leaktest program that started it all.
4. Firewall Leak Tester - www.firewallleaktester.com
This is a one-stop shop for leak tester programs you can use to test your software. It has over 26 leak testers. The website published results comparing various firewalls but note that the comparison was done in 2006 so that may have been outdated. You can always download the leak testers and test individually.
Other leak testers are:
http://www.pcflank.com/pcflankleaktest.htm
(this article can be seen at
www.pckingsford.com)
5. Testing exploits to PC.
http://www.pcflank.com/exploits.htm - simulates Denial of Service attacks on your system.
6. Question on "How Does a Router Protect?" has some answers here:
http://xtechnotes.blogspot.com.au/2012/04/how-does-router-protect.html
7. Linux - IPTABLES
For Linux users, the software firewall IPTABLES allow maximum configurability.
More details can be found in the "Linux Firewall" section in: http://xtechnotes.blogspot.com.au/2012/05/notes-linux.html
A list of simple rules is given here as an example for
Super Stealth mode
----------------
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
----------------
Once you've executed them, use this command for the stealth config to stick:
Code:
service iptables save
The above rules are quite strict. For simple web browsing, try this:
Basic Web Browsing mode
-----------------------------------
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -j DROP
iptables -A OUTPUT -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables-save > /etc/sysconfig/iptables
----------------------------------
then to restart:
service iptables save
service iptables start
Links to IPTABLES configuration:
http://www.thegeekstuff.com/2011/06/iptables-rules-examples/
http://www.thegeekstuff.com/2012/08/iptables-log-packets/
http://www.thegeekstuff.com/2011/01/redhat-iptables-flush/
http://www.thegeekstuff.com/2011/03/iptables-inbound-and-outbound-rules/
http://www.thegeekstuff.com/2011/02/iptables-add-rule/
http://www.thegeekstuff.com/2011/01/iptables-fundamentals/
1. www.pcflank.com
Contains various test for the firewall including: port scanners, stealth testing, leak test and others.
For leak test, download PCFlankLeaktest.exe. This test tries to send some information out of your PC to the PCFlank site. The results are shown in http://www.pcflank.com/pcflankleaktest_results.htm
. According to PCFlank, only Outpost Firewall Pro and Tiny Personal Firewall pass the leaktest. At PCKingsford, we have tried the FREE Comodo Firewall Pro (CFP) and this works. If it fails, you just need to be sure that you have not tell CFP to ALLOW it. To check whether an application is allowed in CFP, open up the CFP, go to the Defense+ on top, then go to the Advanced tab on the left menu, then click Computer Security Policy. Look for the application name and edit the rules for it.
2. ShieldsUp at http://www.grc.com/faq-shieldsup.htm
Click on Services tab on the website, then select ShieldsUp. Follow the instruction to test your firewall via this website. The tests include:
File Sharing
Common Ports
All Service Ports
Messenger Spam
Browser Helpers
3. Leaktest 1.2 at http://www.grc.com/lt/leaktest.htm This is one of the original firewall leaktest program that started it all.
4. Firewall Leak Tester - www.firewallleaktester.com
This is a one-stop shop for leak tester programs you can use to test your software. It has over 26 leak testers. The website published results comparing various firewalls but note that the comparison was done in 2006 so that may have been outdated. You can always download the leak testers and test individually.
Other leak testers are:
http://www.pcflank.com/pcflankleaktest.htm
(this article can be seen at
www.pckingsford.com)
5. Testing exploits to PC.
http://www.pcflank.com/exploits.htm - simulates Denial of Service attacks on your system.
6. Question on "How Does a Router Protect?" has some answers here:
http://xtechnotes.blogspot.com.au/2012/04/how-does-router-protect.html
7. Linux - IPTABLES
For Linux users, the software firewall IPTABLES allow maximum configurability.
More details can be found in the "Linux Firewall" section in: http://xtechnotes.blogspot.com.au/2012/05/notes-linux.html
A list of simple rules is given here as an example for
Super Stealth mode
----------------
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -F
iptables -X
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
----------------
Once you've executed them, use this command for the stealth config to stick:
Code:
service iptables save
The above rules are quite strict. For simple web browsing, try this:
Basic Web Browsing mode
-----------------------------------
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -j DROP
iptables -A OUTPUT -j ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables-save > /etc/sysconfig/iptables
----------------------------------
then to restart:
service iptables save
service iptables start
Links to IPTABLES configuration:
http://www.thegeekstuff.com/2011/06/iptables-rules-examples/
http://www.thegeekstuff.com/2012/08/iptables-log-packets/
http://www.thegeekstuff.com/2011/01/redhat-iptables-flush/
http://www.thegeekstuff.com/2011/03/iptables-inbound-and-outbound-rules/
http://www.thegeekstuff.com/2011/02/iptables-add-rule/
http://www.thegeekstuff.com/2011/01/iptables-fundamentals/
Labels:
comodo,
firewall hardening,
grc,
leaktest,
outpost,
pcflank,
secure ports,
security
Tuesday, December 25, 2007
Notes Matlab
Notes Matlab
============
Contents
=========
Help
MEX - C
MEX - C
========
A list of mx-MEX functions is found in:
Help -> MATLAB -> C and Fortran Functions
Mex interface in C code:
******************
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* more C code ... */
******************
Help:
=====
>matlab -help
Setup Matlab's path - save the following into startup.m
*********************
% Add my test directory as the first entry
% on MATLAB's search path
path('/import/george/Applied/chee/programs/RKohn/SallyW',path)
*********************
To Run Matlab m-file, just type the name WITHOUT the ".m"
eg to run Hello.m, type "Hello"
Running Matlab:
1. GUI environment: /usr/local/matlab/bin/matlab
2. NO GUI: /usr/local/matlab/bin/matlabl -nojvm
*********************
RESHAPE
>> crshp(:,:,1)=[1,2,3,4;5,6,7,8;9,10,11,12]
>> crshp(:,:,2)=[13,14,15,16;17,18,19,20;21,22,23,24]
crshp(:,:,1) =
1 2 3 4
5 6 7 8
9 10 11 12
crshp(:,:,2) =
13 14 15 16
17 18 19 20
21 22 23 24
>> reshape(crshp(:,:,1), 12, 1)
ans =
1
5
9
2
6
10
3
7
11
4
8
12
============
Contents
=========
Help
MEX - C
MEX - C
========
A list of mx-MEX functions is found in:
Help -> MATLAB -> C and Fortran Functions
Mex interface in C code:
******************
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* more C code ... */
******************
Help:
=====
>matlab -help
Setup Matlab's path - save the following into startup.m
*********************
% Add my test directory as the first entry
% on MATLAB's search path
path('/import/george/Applied/chee/programs/RKohn/SallyW',path)
*********************
To Run Matlab m-file, just type the name WITHOUT the ".m"
eg to run Hello.m, type "Hello"
Running Matlab:
1. GUI environment: /usr/local/matlab/bin/matlab
2. NO GUI: /usr/local/matlab/bin/matlabl -nojvm
*********************
RESHAPE
>> crshp(:,:,1)=[1,2,3,4;5,6,7,8;9,10,11,12]
>> crshp(:,:,2)=[13,14,15,16;17,18,19,20;21,22,23,24]
crshp(:,:,1) =
1 2 3 4
5 6 7 8
9 10 11 12
crshp(:,:,2) =
13 14 15 16
17 18 19 20
21 22 23 24
>> reshape(crshp(:,:,1), 12, 1)
ans =
1
5
9
2
6
10
3
7
11
4
8
12
Subscribe to:
Comments (Atom)