Herox.net Weblog

Weblog von Marcel Gädigk

OpenCL testfiles in nem projekt

Januar 18th, 2010 by sqisch

http://herox.net/tmp/opencl_tryout.tar.gz

starten mit:

make
make examples
cd examples
./main

oder ./main2

oder ./helloMulti

;)

OpenCL C++ Multiple GPU Hello World example

Januar 18th, 2010 by sqisch

Simple OpenCL C++ example for computing the square on all GPUs
You can find the header file here: here

main.cpp

/*
 * gtx 280
 * tesla c1060
 */
#define __CL_ENABLE_EXCEPTIONS
//#define __NO_STD_VECTOR
//#define __NO_STD_STRING

#include
#include
#include
#include
#include "cl.hpp"

const unsigned int BLOCK_N = 512;
const unsigned int THREAD_N = 512;
const unsigned int ACCUM_N = BLOCK_N * THREAD_N;

class Source
{
public:

	Source( const char* filename )
	: m_size(0),
	  m_source(0)
	{
		std::ifstream file( filename,
                            std::ios::in | std::ios::binary | std::ios::ate );
		if( file.is_open() ) {
			m_size = size_t(file.tellg()) + 1;
			m_source = new char[m_size];
			file.seekg( 0, std::ios::beg );
			file.read( m_source, m_size-1 );
			file.close();
			m_source[m_size-1] = '\0';
		}
	}

	virtual ~Source()
	{
		if( m_source ) delete [] m_source;
	}

	const char* get()
	{
		return m_source;
	}

	std::pair< char*, size_t > getPair()
	{
		return std::make_pair( m_source, m_size );
	}

	size_t size()
	{
		return m_size;
	}

private:

	size_t m_size;
	char*  m_source;

};

int main( int argc, char** argv )
{

	unsigned int count = 65536;
	size_t memSize = sizeof(float)*count;

	float data[count];
	float results[count];

	for( unsigned int i=0; i < count; ++i )
		data[i] = rand()/(float)RAND_MAX;

	Source source("square.cl");
	std::cout << source.get();

	try {
		cl::Context context( CL_DEVICE_TYPE_GPU );

		std::vector devices = context.getInfo();
		std::cout << "I Found " << devices.size() << " OpenCL devices" << std::endl;

		cl::Program::Sources clsource( 1, source.getPair() );
		cl::Program program( context, clsource );
		program.build( devices );

		cl_event GPUDone[devices.size()];

	std::vector queues(devices.size());
	cl::Buffer input[devices.size()];
        cl::Buffer output[devices.size()];
	cl::Kernel kernel[devices.size()];
	size_t localSize[devices.size()];

	int sizePerGPU = count / devices.size();
	int workOffset[devices.size()];
   	int workSize[devices.size()];

    	size_t localWorkSize = {THREAD_N};
    	size_t globalWorkSize = {ACCUM_N};    

    	workOffset[0] = 0;
   for(unsigned int i = 0; i < devices.size(); ++i )
   {
		workSize[i] = (i != (devices.size() - 1)) ? sizePerGPU : (count - workOffset[i]);        

		queues[i] = cl::CommandQueue( context, devices[i] );

		input[i] = cl::Buffer(context, CL_MEM_READ_ONLY, workSize[i] * sizeof(float));
		output[i] = cl::Buffer(context, CL_MEM_WRITE_ONLY, workSize[i] * sizeof(float));

		queues[i].enqueueWriteBuffer( input[i], CL_TRUE, 0, workSize[i] * sizeof(float) , &data[workOffset[i]] );

		kernel[i] = cl::Kernel( program, "square" );

		localSize[i] = kernel[i].getWorkGroupInfo< CL_KERNEL_WORK_GROUP_SIZE >(devices[i] );

		cl::KernelFunctor func = kernel[i].bind(
				queues[i],
				cl::NDRange(globalWorkSize),
				cl::NDRange(localWorkSize) );

		func( input[i], output[i], workSize[i] ).wait();

		queues[i].enqueueReadBuffer( output[i], CL_TRUE, 0, workSize[i] * sizeof(float), &results[workOffset[i]] );

		if(i==devices.size()-1)
			break;
		else
       			workOffset[i + 1] = workOffset[i] + workSize[i];

}

	} catch( cl::Error error ) {

		std::cerr
			<<"ERROR: "
			<< error.what()
			<< "("
			<< error.err()
			<< ")"
			<< std::endl;
	}	

	unsigned int correct = 0;
	for( unsigned int i = 0; i < count; ++i ) {
		if( results[i] == data[i]*data[i] )
			++correct;
	}

	std::cout << "Computed " << correct << "/" << count;
	std::cout << " correct values." << std::endl;

	return 0;
}

kernel:
square.cl

A simple kernel:

__kernel void square(
        __global float* input,
        __global float* output,
        const unsigned int count
)
{
        const size_t id = get_global_id(0);
        if( id < count )
                output[id] = input[id]*input[id];
}

OpenCL C++ HelloWorld example

Januar 13th, 2010 by sqisch

Simple OpenCL C++ example for computing the square on one GPU
You can find the header file here: here
main.cpp

/*
 * gtx 280
 * tesla c1060
 */

#define __CL_ENABLE_EXCEPTIONS
//#define __NO_STD_VECTOR
//#define __NO_STD_STRING

#include 
#include 
#include 
#include 
#include "cl.hpp"

class Source
{
public:

        Source( const char* filename )
        : m_size(0),
          m_source(0)
        {
                std::ifstream file( filename,
                            std::ios::in | std::ios::binary | std::ios::ate );
                if( file.is_open() ) {
                        m_size = size_t(file.tellg()) + 1;
                        m_source = new char[m_size];
                        file.seekg( 0, std::ios::beg );
                        file.read( m_source, m_size-1 );
                        file.close();
                        m_source[m_size-1] = '\0';
                }
        }

        virtual ~Source()
        {
                if( m_source ) delete [] m_source;
        }

        const char* get()
        {
                return m_source;
        }

        std::pair< char*, size_t > getPair()
        {
                return std::make_pair( m_source, m_size );
        }

        size_t size()
        {
                return m_size;
        }

private:

        size_t m_size;
        char*  m_source;

};

int main( int argc, char** argv )
{

        unsigned int count = 1024;
        size_t memSize = sizeof(float)*count;

        float data[count];
        float results[count];

        for( unsigned int i=0; i < count; ++i )
                data[i] = rand()/(float)RAND_MAX;

        Source source("square.cl");
        std::cout << source.get();

        try {

                cl::Context context( CL_DEVICE_TYPE_GPU );

                std::vector devices = context.getInfo();

                cl::Program::Sources clsource( 1, source.getPair() );
                cl::Program program( context, clsource );
                program.build( devices );

                cl::CommandQueue queue( context, devices[0] );

                cl::Buffer input( context, CL_MEM_READ_ONLY, memSize );
                cl::Buffer output( context, CL_MEM_WRITE_ONLY, memSize );

                queue.enqueueWriteBuffer( input, CL_TRUE, 0, memSize, data );

                cl::Kernel kernel( program, "square" );

                //kernel.setArg( 0, input );
                //kernel.setArg( 1, output );
                //kernel.setArg( 2, count );

                size_t localSize = kernel.getWorkGroupInfo< CL_KERNEL_WORK_GROUP_SIZE >(
                                devices[0] );

                cl::KernelFunctor func = kernel.bind(
                                queue,
                                cl::NDRange(count),
                                cl::NDRange(localSize) );

                func( input, output, count ).wait();

                queue.enqueueReadBuffer( output, CL_TRUE, 0, memSize, results );

        } catch( cl::Error error ) {

                std::cerr
                        <<"ERROR: "
                        << error.what()
                        << "("
                        << error.err()
                        << ")"
                        << std::endl;

        }

        unsigned int correct = 0;
        for( unsigned int i = 0; i < count; ++i ) {
                if( results[i] == data[i]*data[i] )
                        ++correct;
        }
        std::cout << "Computed " << correct << "/" << count;
        std::cout << " correct values." << std::endl;

        return 0;
}

A simple kernel:

__kernel void square(
        __global float* input,
        __global float* output,
        const unsigned int count
)
{
        const size_t id = get_global_id(0);
        if( id < count )
                output[id] = input[id]*input[id];
}

Angeblicher fail einer Russischen Testrakete

Dezember 15th, 2009 by sqisch

http://www.wired.com/images_blogs/wiredscience/2009/12/jan-petter1-660x490.jpg

http://www.wired.com/images_blogs/wiredscience/2009/12/jan-petter1-660x490.jpg

Bei solchen bildern wird mir manchmal mulmig,was zur hölle das war.

Python OpenCV Sampel1: open two Camera Windows.

Dezember 10th, 2009 by sqisch

Open two Webcam windows.One with Original Image.

from opencv import cv
from opencv import highgui
import sys

image = None

if __name__ == '__main__':

    try:
        # try to get the device number from the command line
        device = int (sys.argv [1])
        del sys.argv [1]
    except (IndexError, ValueError):
        # no device number on the command line, assume we want the 1st device
        device = 0
    if len (sys.argv) == 1:
        # no argument on the command line, try to use the camera
        capture = highgui.cvCreateCameraCapture (device)

    else:
        # we have an argument on the command line,
        # we can assume this is a file name, so open it
        capture = highgui.cvCreateFileCapture (sys.argv [1])  

    if not capture:
        print "Error opening capture device"
        sys.exit (1)

    # first, create the necessary windows
    highgui.cvNamedWindow ('LkDemo', highgui.CV_WINDOW_AUTOSIZE)
    highgui.cvNamedWindow ('LkDemo2', highgui.CV_WINDOW_AUTOSIZE)
    while 1:
        frame = highgui.cvQueryFrame (capture)
        frame2 = highgui.cvQueryFrame (capture)
        if frame is None:
            # no image captured... end the processing
            break
        if image is None:
            image = cv.cvCreateImage (cv.cvGetSize (frame), 8, 3)
            grey = cv.cvCreateImage (cv.cvGetSize (frame2), 8, 1)
            highgui.cvMoveWindow('LkDemo2',int(highgui.cvGetCaptureProperty(capture,highgui.CV_CAP_PROP_FRAME_WIDTH)),0);
            image.origin = frame.origin

        cv.cvCopy (frame, image)
        cv.cvCopy (frame2, image)

        cv.cvCvtColor (image, grey, cv.CV_BGR2GRAY)
        print "image"
        highgui.cvShowImage ('LkDemo', image)
        highgui.cvShowImage ('LkDemo2', grey)

        c = highgui.cvWaitKey (10)

Google.de suche nach Google.de

Oktober 20th, 2009 by sqisch

Wenn man auf google.de nach google.de sucht kommt komischweiße ein video von 23C3 an vierter stelle vor ALLEN google tools/links….

Warum? -> keine ahnung. Witzig finde ich es trotzdem.

vegansky.de

August 31st, 2009 by sqisch

Vegansky.de ist eine Suchmaschine für vegane Produkte. Man gibt z.B. das sticwort Käse ein und es wird eine vielzahl an Veganen Produkten aufgelistet, die Käseähnlich sind.

Suchmaschine für vegane Produkte

.htaccess ldap htw-dresden Zugriffschutz

Juni 21st, 2009 by sqisch

Falls ihr nur HTW-Studenten auf euren Studentenseiten haben wollt hier eine .htaccess datei.
Das ganze geht allerdings nur für den webserver informatik.htw-dresden.de. Warum es vom rob aus nicht geht keine ahnung.

AuthType Basic
AuthName LDAP-Authentifizierung
AuthBasicProvider ldap
AuthzLDAPAuthoritative off
#LDAP-Server
AuthLDAPURL ldap://ildap1.informatik.htw-dresden.de/o=informatik,dc=htwdd?uid
require valid-user

Es ist gut!!!

Juni 8th, 2009 by sqisch

scharfschützen auf dresdens dächern
scharfi2
Scharfschützen auf Dresdens Sehenswürdigkeiten!!

http://www.mdr.de/I/6418698.jpg
Mit wunderschönen blick auf die Frauenkirche

http://www.mdr.de/I/6418688.jpg
und das alles nur für den gefährdesten menschen der welt…

Humanoider Roboter Manoi

Mai 23rd, 2009 by sqisch

Nur geil, was für bewegungen die roboter inzwischen können.

« Previous Entries