Posts Tagged ‘qt’

meshlab filter console output missing

Tuesday, May 10th, 2011

I upgraded to MeshLab version 1.3 and was frustrated that when I issued filters on a mesh like “Remove Duplicate Vertices” there was no longer a console output listing the results of the filter ( in this case how many vertices were removed).

The console apparently only appears if you have the Layers dialog open. So go to View > Show Layer Dialog, the console shows up at the bottom.

meshlab filter console

What’s really annoying is that when you change the focus away from mesh lab and then back to mesh lab the Layers Dialog conveniently closes itself. So I have to keep opening and reopening it.

Update: It seems that the bug that makes the Layers Dialog disappear only happens if the Layers Dialog is locked to the right of the main window. If you drag the top of the dialog completely outside of the main meshlab window and leave it dangling some place then it doesn’t disappear.

Yet another macports/qt/mac nightmare…

Friday, May 6th, 2011

Today I wasted countless hours recompiling all sorts of junk on my computer. The culprits once again are macports, Qt and Apple’s 32-bit/64-bit androgyny.

I recently upgraded from mac os x 10.5 (where 32-bits is default) to mac os x 10.6 (where 64-bits is default). Macports naturally pretends to work just fine after I make the switch. Low and behold it’s little house on the sand is about to wash away. Today I wanted to upgrade to the latest version of pdflatex. I tried to do so using macports. No go. Eventually I found the “migration” guide for “migrating” you macports after you upgrade from mac os x 10.5 to mac os x 10.6. Their idea of “migration” is uninstalling and reinstalling everything! All of my 300 some ports needed to be recompiled. Their instructions by the way did not work as it didn’t account for external dependencies still hanging around in i386, 32-bit mode.

I’ve finally reinstalled pdflatex. But now my Qt installation doesn’t work.

I tried:


sudo port install qt4-mac

and about a year later everything seemed to have worked fine. Kudos to macports for having the bright idea to restrain from distributing precompiled binaries. I’m learning patience!

But everything did not work fine. I re-qmake-ed my current project into an xcode project which went fine, but when I tried to build my project in xcode I got the following mysterious error:


pbxcp: warning: couldn't strip: /absolute/path/to/my/app: No such file or directory

Searching around the only ideas I found were to delete the build directory, clean and rebuild which did not work.

Solution:


sudo port deactivate qt4-mac

Install Qt SDK directly from Qt site (hours faster than macports by the way).

Combining CUDA, Qt, and Xcode

Monday, November 29th, 2010

As a proof of concept and a skeleton for some more intense code, I wanted to be sure that I could get a simple example program that used CUDA, Qt and Xcode. A priori there is no reason that this shouldn’t work.

The simplest way to do this is to separate CUDA from the main program entirely. I found a discussion of how to do this on the nvidia site. I will base my example on the final snipets on that thread.

Building a CUDA library

The first step will be to bake my gpu code into a static library. The idea is then to call that library from my Qt main program.

This involved five files.

HelloWorld.cu:


#include "HelloWorld.cuh"
#include <stdio.h>

// Kernel functions must be inlined (?)
#include "cuPrintf.cu"
__global__ void HelloFromDevice(void)
{
  cuPrintf("Hello, world from block %d thread %d!\n",blockIdx.x,threadIdx.x);
}

int HelloWorld()
{
  // greet from the host
  printf("Hello, world from the host!\n");

  // initialize cuPrintf
  cudaPrintfInit();

  // launch a kernel with a single thread to greet from the device
  HelloFromDevice<<<10,64>>>();

  // display the device's greeting
  cudaPrintfDisplay();
  
  // clean up after cuPrintf
  cudaPrintfEnd();

  return 0;
}

HelloWorld.cuh


int HelloWorld();

cuPrintf.cu (by Nvidia)


/*
	Copyright 2009 NVIDIA Corporation.  All rights reserved.

	NOTICE TO LICENSEE:   

	This source code and/or documentation ("Licensed Deliverables") are subject 
	to NVIDIA intellectual property rights under U.S. and international Copyright 
	laws.  

	These Licensed Deliverables contained herein is PROPRIETARY and CONFIDENTIAL 
	to NVIDIA and is being provided under the terms and conditions of a form of 
	NVIDIA software license agreement by and between NVIDIA and Licensee ("License 
	Agreement") or electronically accepted by Licensee.  Notwithstanding any terms 
	or conditions to the contrary in the License Agreement, reproduction or 
	disclosure of the Licensed Deliverables to any third party without the express 
	written consent of NVIDIA is prohibited.     

	NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE LICENSE AGREEMENT, 
	NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THESE LICENSED 
	DELIVERABLES FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED 
	WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE 
	LICENSED DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, 
	NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.   NOTWITHSTANDING ANY 
	TERMS OR CONDITIONS TO THE CONTRARY IN THE LICENSE AGREEMENT, IN NO EVENT SHALL 
	NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, 
	OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,	WHETHER 
	IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF 
	OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THESE LICENSED DELIVERABLES.  

	U.S. Government End Users. These Licensed Deliverables are a "commercial item" 
	as that term is defined at  48 C.F.R. 2.101 (OCT 1995), consisting  of 
	"commercial computer  software"  and "commercial computer software documentation" 
	as such terms are  used in 48 C.F.R. 12.212 (SEPT 1995) and is provided to the 
	U.S. Government only as a commercial end item.  Consistent with 48 C.F.R.12.212 
	and 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all U.S. Government 
	End Users acquire the Licensed Deliverables with only those rights set forth 
	herein. 

	Any use of the Licensed Deliverables in individual and commercial software must 
	include, in the user documentation and internal comments to the code, the above 
	Disclaimer and U.S. Government End Users Notice.
 */

/*
 *	cuPrintf.cu
 *
 *	This is a printf command callable from within a kernel. It is set
 *	up so that output is sent to a memory buffer, which is emptied from
 *	the host side - but only after a cudaThreadSynchronize() on the host.
 *
 *	Currently, there is a limitation of around 200 characters of output
 *	and no more than 10 arguments to a single cuPrintf() call. Issue
 *	multiple calls if longer format strings are required.
 *
 *	It requires minimal setup, and is *NOT* optimised for performance.
 *	For example, writes are not coalesced - this is because there is an
 *	assumption that people will not want to printf from every single one
 *	of thousands of threads, but only from individual threads at a time.
 *
 *	Using this is simple - it requires one host-side call to initialise
 *	everything, and then kernels can call cuPrintf at will. Sample code
 *	is the easiest way to demonstrate:
 *
	#include "cuPrintf.cu"
 	
	__global__ void testKernel(int val)
	{
		cuPrintf("Value is: %d\n", val);
	}

	int main()
	{
		cudaPrintfInit();
		testKernel<<< 2, 3 >>>(10);
		cudaPrintfDisplay(stdout, true);
		cudaPrintfEnd();
        return 0;
	}
 *
 *	See the header file, "cuPrintf.cuh" for more info, especially
 *	arguments to cudaPrintfInit() and cudaPrintfDisplay();
 */

#ifndef CUPRINTF_CU
#define CUPRINTF_CU

#include "cuPrintf.cuh"
#if __CUDA_ARCH__ > 100      // Atomics only used with > sm_10 architecture
#include <sm_11_atomic_functions.h>
#endif

// This is the smallest amount of memory, per-thread, which is allowed.
// It is also the largest amount of space a single printf() can take up
const static int CUPRINTF_MAX_LEN = 256;

// This structure is used internally to track block/thread output restrictions.
typedef struct __align__(8) {
	int threadid;				// CUPRINTF_UNRESTRICTED for unrestricted
	int blockid;				// CUPRINTF_UNRESTRICTED for unrestricted
} cuPrintfRestriction;

// The main storage is in a global print buffer, which has a known
// start/end/length. These are atomically updated so it works as a
// circular buffer.
// Since the only control primitive that can be used is atomicAdd(),
// we cannot wrap the pointer as such. The actual address must be
// calculated from printfBufferPtr by mod-ing with printfBufferLength.
// For sm_10 architecture, we must subdivide the buffer per-thread
// since we do not even have an atomic primitive.
__constant__ static char *globalPrintfBuffer = NULL;         // Start of circular buffer (set up by host)
__constant__ static int printfBufferLength = 0;              // Size of circular buffer (set up by host)
__device__ static cuPrintfRestriction restrictRules;         // Output restrictions
__device__ volatile static char *printfBufferPtr = NULL;     // Current atomically-incremented non-wrapped offset

// This is the header preceeding all printf entries.
// NOTE: It *must* be size-aligned to the maximum entity size (size_t)
typedef struct __align__(8) {
    unsigned short magic;                   // Magic number says we're valid
    unsigned short fmtoffset;               // Offset of fmt string into buffer
    unsigned short blockid;                 // Block ID of author
    unsigned short threadid;                // Thread ID of author
} cuPrintfHeader;

// Special header for sm_10 architecture
#define CUPRINTF_SM10_MAGIC   0xC810        // Not a valid ascii character
typedef struct __align__(16) {
    unsigned short magic;                   // sm_10 specific magic number
    unsigned short unused;
    unsigned int thread_index;              // thread ID for this buffer
    unsigned int thread_buf_len;            // per-thread buffer length
    unsigned int offset;                    // most recent printf's offset
} cuPrintfHeaderSM10;


// Because we can't write an element which is not aligned to its bit-size,
// we have to align all sizes and variables on maximum-size boundaries.
// That means sizeof(double) in this case, but we'll use (long long) for
// better arch<1.3 support
#define CUPRINTF_ALIGN_SIZE      sizeof(long long)

// All our headers are prefixed with a magic number so we know they're ready
#define CUPRINTF_SM11_MAGIC  (unsigned short)0xC811        // Not a valid ascii character


//
//  getNextPrintfBufPtr
//
//  Grabs a block of space in the general circular buffer, using an
//  atomic function to ensure that it's ours. We handle wrapping
//  around the circular buffer and return a pointer to a place which
//  can be written to.
//
//  Important notes:
//      1. We always grab CUPRINTF_MAX_LEN bytes
//      2. Because of 1, we never worry about wrapping around the end
//      3. Because of 1, printfBufferLength *must* be a factor of CUPRINTF_MAX_LEN
//
//  This returns a pointer to the place where we own.
//
__device__ static char *getNextPrintfBufPtr()
{
    // Initialisation check
    if(!printfBufferPtr)
        return NULL;

	// Thread/block restriction check
	if((restrictRules.blockid != CUPRINTF_UNRESTRICTED) && (restrictRules.blockid != (blockIdx.x + gridDim.x*blockIdx.y)))
		return NULL;
	if((restrictRules.threadid != CUPRINTF_UNRESTRICTED) && (restrictRules.threadid != (threadIdx.x + blockDim.x*threadIdx.y + blockDim.x*blockDim.y*threadIdx.z)))
		return NULL;

	// Conditional section, dependent on architecture
#if __CUDA_ARCH__ == 100
    // For sm_10 architectures, we have no atomic add - this means we must split the
    // entire available buffer into per-thread blocks. Inefficient, but what can you do.
    int thread_count = (gridDim.x * gridDim.y) * (blockDim.x * blockDim.y * blockDim.z);
    int thread_index = threadIdx.x + blockDim.x*threadIdx.y + blockDim.x*blockDim.y*threadIdx.z +
                       (blockIdx.x + gridDim.x*blockIdx.y) * (blockDim.x * blockDim.y * blockDim.z);
    
    // Find our own block of data and go to it. Make sure the per-thread length
	// is a precise multiple of CUPRINTF_MAX_LEN, otherwise we risk size and
	// alignment issues! We must round down, of course.
    unsigned int thread_buf_len = printfBufferLength / thread_count;
	thread_buf_len &= ~(CUPRINTF_MAX_LEN-1);

	// We *must* have a thread buffer length able to fit at least two printfs (one header, one real)
	if(thread_buf_len < (CUPRINTF_MAX_LEN * 2))
		return NULL;

	// Now address our section of the buffer. The first item is a header.
    char *myPrintfBuffer = globalPrintfBuffer + (thread_buf_len * thread_index);
    cuPrintfHeaderSM10 hdr = *(cuPrintfHeaderSM10 *)(void *)myPrintfBuffer;
    if(hdr.magic != CUPRINTF_SM10_MAGIC)
    {
        // If our header is not set up, initialise it
        hdr.magic = CUPRINTF_SM10_MAGIC;
        hdr.thread_index = thread_index;
        hdr.thread_buf_len = thread_buf_len;
        hdr.offset = 0;         // Note we start at 0! We pre-increment below.
        *(cuPrintfHeaderSM10 *)(void *)myPrintfBuffer = hdr;       // Write back the header

        // For initial setup purposes, we might need to init thread0's header too
        // (so that cudaPrintfDisplay() below will work). This is only run once.
        cuPrintfHeaderSM10 *tophdr = (cuPrintfHeaderSM10 *)(void *)globalPrintfBuffer;
        tophdr->thread_buf_len = thread_buf_len;
    }

    // Adjust the offset by the right amount, and wrap it if need be
    unsigned int offset = hdr.offset + CUPRINTF_MAX_LEN;
    if(offset >= hdr.thread_buf_len)
        offset = CUPRINTF_MAX_LEN;

    // Write back the new offset for next time and return a pointer to it
    ((cuPrintfHeaderSM10 *)(void *)myPrintfBuffer)->offset = offset;
    return myPrintfBuffer + offset;
#else
    // Much easier with an atomic operation!
    size_t offset = atomicAdd((unsigned int *)&printfBufferPtr, CUPRINTF_MAX_LEN) - (size_t)globalPrintfBuffer;
    offset %= printfBufferLength;
    return globalPrintfBuffer + offset;
#endif
}


//
//  writePrintfHeader
//
//  Inserts the header for containing our UID, fmt position and
//  block/thread number. We generate it dynamically to avoid
//	issues arising from requiring pre-initialisation.
//
__device__ static void writePrintfHeader(char *ptr, char *fmtptr)
{
    if(ptr)
    {
        cuPrintfHeader header;
        header.magic = CUPRINTF_SM11_MAGIC;
        header.fmtoffset = (unsigned short)(fmtptr - ptr);
        header.blockid = blockIdx.x + gridDim.x*blockIdx.y;
        header.threadid = threadIdx.x + blockDim.x*threadIdx.y + blockDim.x*blockDim.y*threadIdx.z;
        *(cuPrintfHeader *)(void *)ptr = header;
    }
}


//
//  cuPrintfStrncpy
//
//  This special strncpy outputs an aligned length value, followed by the
//  string. It then zero-pads the rest of the string until a 64-aligned
//  boundary. The length *includes* the padding. A pointer to the byte
//  just after the \0 is returned.
//
//  This function could overflow CUPRINTF_MAX_LEN characters in our buffer.
//  To avoid it, we must count as we output and truncate where necessary.
//
__device__ static char *cuPrintfStrncpy(char *dest, const char *src, int n, char *end)
{
    // Initialisation and overflow check
    if(!dest || !src || (dest >= end))
        return NULL;

    // Prepare to write the length specifier. We're guaranteed to have
    // at least "CUPRINTF_ALIGN_SIZE" bytes left because we only write out in
    // chunks that size, and CUPRINTF_MAX_LEN is aligned with CUPRINTF_ALIGN_SIZE.
    int *lenptr = (int *)(void *)dest;
    int len = 0;
    dest += CUPRINTF_ALIGN_SIZE;

    // Now copy the string
    while(n--)
    {
        if(dest >= end)     // Overflow check
            break;

        len++;
        *dest++ = *src;
        if(*src++ == '\0')
            break;
    }

    // Now write out the padding bytes, and we have our length.
    while((dest < end) && (((long)dest & (CUPRINTF_ALIGN_SIZE-1)) != 0))
    {
        len++;
        *dest++ = 0;
    }
    *lenptr = len;
    return (dest < end) ? dest : NULL;        // Overflow means return NULL
}


//
//  copyArg
//
//  This copies a length specifier and then the argument out to the
//  data buffer. Templates let the compiler figure all this out at
//  compile-time, making life much simpler from the programming
//  point of view. I'm assuimg all (const char *) is a string, and
//  everything else is the variable it points at. I'd love to see
//  a better way of doing it, but aside from parsing the format
//  string I can't think of one.
//
//  The length of the data type is inserted at the beginning (so that
//  the display can distinguish between float and double), and the
//  pointer to the end of the entry is returned.
//
__device__ static char *copyArg(char *ptr, const char *arg, char *end)
{
    // Initialisation check
    if(!ptr || !arg)
        return NULL;

    // strncpy does all our work. We just terminate.
    if((ptr = cuPrintfStrncpy(ptr, arg, CUPRINTF_MAX_LEN, end)) != NULL)
        *ptr = 0;

    return ptr;
}

template <typename T>
__device__ static char *copyArg(char *ptr, T &arg, char *end)
{
    // Initisalisation and overflow check. Alignment rules mean that
    // we're at least CUPRINTF_ALIGN_SIZE away from "end", so we only need
    // to check that one offset.
    if(!ptr || ((ptr+CUPRINTF_ALIGN_SIZE) >= end))
        return NULL;

    // Write the length and argument
    *(int *)(void *)ptr = sizeof(arg);
    ptr += CUPRINTF_ALIGN_SIZE;
    *(T *)(void *)ptr = arg;
    ptr += CUPRINTF_ALIGN_SIZE;
    *ptr = 0;

    return ptr;
}


//
//  cuPrintf
//
//  Templated printf functions to handle multiple arguments.
//  Note we return the total amount of data copied, not the number
//  of characters output. But then again, who ever looks at the
//  return from printf() anyway?
//
//  The format is to grab a block of circular buffer space, the
//  start of which will hold a header and a pointer to the format
//  string. We then write in all the arguments, and finally the
//  format string itself. This is to make it easy to prevent
//  overflow of our buffer (we support up to 10 arguments, each of
//  which can be 12 bytes in length - that means that only the
//  format string (or a %s) can actually overflow; so the overflow
//  check need only be in the strcpy function.
//
//  The header is written at the very last because that's what
//  makes it look like we're done.
//
//  Errors, which are basically lack-of-initialisation, are ignored
//  in the called functions because NULL pointers are passed around
//

// All printf variants basically do the same thing, setting up the
// buffer, writing all arguments, then finalising the header. For
// clarity, we'll pack the code into some big macros.
#define CUPRINTF_PREAMBLE \
    char *start, *end, *bufptr, *fmtstart; \
    if((start = getNextPrintfBufPtr()) == NULL) return 0; \
    end = start + CUPRINTF_MAX_LEN; \
    bufptr = start + sizeof(cuPrintfHeader);

// Posting an argument is easy
#define CUPRINTF_ARG(argname) \
	bufptr = copyArg(bufptr, argname, end);

// After args are done, record start-of-fmt and write the fmt and header
#define CUPRINTF_POSTAMBLE \
    fmtstart = bufptr; \
    end = cuPrintfStrncpy(bufptr, fmt, CUPRINTF_MAX_LEN, end); \
    writePrintfHeader(start, end ? fmtstart : NULL); \
    return end ? (int)(end - start) : 0;

__device__ int cuPrintf(const char *fmt)
{
	CUPRINTF_PREAMBLE;

	CUPRINTF_POSTAMBLE;
}
template <typename T1> __device__ int cuPrintf(const char *fmt, T1 arg1)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);
	CUPRINTF_ARG(arg5);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);
	CUPRINTF_ARG(arg5);
	CUPRINTF_ARG(arg6);
	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);
	CUPRINTF_ARG(arg5);
	CUPRINTF_ARG(arg6);
	CUPRINTF_ARG(arg7);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
{
	CUPRINTF_PREAMBLE;

	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);
	CUPRINTF_ARG(arg5);
	CUPRINTF_ARG(arg6);
	CUPRINTF_ARG(arg7);
	CUPRINTF_ARG(arg8);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);
	CUPRINTF_ARG(arg5);
	CUPRINTF_ARG(arg6);
	CUPRINTF_ARG(arg7);
	CUPRINTF_ARG(arg8);
	CUPRINTF_ARG(arg9);

	CUPRINTF_POSTAMBLE;
}
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
{
	CUPRINTF_PREAMBLE;
	    
	CUPRINTF_ARG(arg1);
	CUPRINTF_ARG(arg2);
	CUPRINTF_ARG(arg3);
	CUPRINTF_ARG(arg4);
	CUPRINTF_ARG(arg5);
	CUPRINTF_ARG(arg6);
	CUPRINTF_ARG(arg7);
	CUPRINTF_ARG(arg8);
	CUPRINTF_ARG(arg9);
	CUPRINTF_ARG(arg10);

	CUPRINTF_POSTAMBLE;
}
#undef CUPRINTF_PREAMBLE
#undef CUPRINTF_ARG
#undef CUPRINTF_POSTAMBLE


//
//	cuPrintfRestrict
//
//	Called to restrict output to a given thread/block.
//	We store the info in "restrictRules", which is set up at
//	init time by the host. It's not the cleanest way to do this
//	because it means restrictions will last between
//	invocations, but given the output-pointer continuity,
//	I feel this is reasonable.
//
__device__ void cuPrintfRestrict(int threadid, int blockid)
{
    int thread_count = blockDim.x * blockDim.y * blockDim.z;
	if(((threadid < thread_count) && (threadid >= 0)) || (threadid == CUPRINTF_UNRESTRICTED))
		restrictRules.threadid = threadid;

	int block_count = gridDim.x * gridDim.y;
	if(((blockid < block_count) && (blockid >= 0)) || (blockid == CUPRINTF_UNRESTRICTED))
		restrictRules.blockid = blockid;
}


///////////////////////////////////////////////////////////////////////////////
// HOST SIDE

#include <stdio.h>
static FILE *printf_fp;

static char *printfbuf_start=NULL;
static char *printfbuf_device=NULL;
static int printfbuf_len=0;


//
//  outputPrintfData
//
//  Our own internal function, which takes a pointer to a data buffer
//  and passes it through libc's printf for output.
//
//  We receive the formate string and a pointer to where the data is
//  held. We then run through and print it out.
//
//  Returns 0 on failure, 1 on success
//
static int outputPrintfData(char *fmt, char *data)
{
    // Format string is prefixed by a length that we don't need
    fmt += CUPRINTF_ALIGN_SIZE;

    // Now run through it, printing everything we can. We must
    // run to every % character, extract only that, and use printf
    // to format it.
    char *p = strchr(fmt, '%');
    while(p != NULL)
    {
        // Print up to the % character
        *p = '\0';
        fputs(fmt, printf_fp);
        *p = '%';           // Put back the %

        // Now handle the format specifier
        char *format = p++;         // Points to the '%'
        p += strcspn(p, "%cdiouxXeEfgGaAnps");
        if(*p == '\0')              // If no format specifier, print the whole thing
        {
            fmt = format;
            break;
        }

        // Cut out the format bit and use printf to print it. It's prefixed
        // by its length.
        int arglen = *(int *)data;
        if(arglen > CUPRINTF_MAX_LEN)
        {
            fputs("Corrupt printf buffer data - aborting\n", printf_fp);
            return 0;
        }

        data += CUPRINTF_ALIGN_SIZE;
        
        char specifier = *p++;
        char c = *p;        // Store for later
        *p = '\0';
        switch(specifier)
        {
            // These all take integer arguments
            case 'c':
            case 'd':
            case 'i':
            case 'o':
            case 'u':
            case 'x':
            case 'X':
            case 'p':
                fprintf(printf_fp, format, *((int *)data));
                break;

            // These all take double arguments
            case 'e':
            case 'E':
            case 'f':
            case 'g':
            case 'G':
            case 'a':
            case 'A':
                if(arglen == 4)     // Float vs. Double thing
                    fprintf(printf_fp, format, *((float *)data));
                else
                    fprintf(printf_fp, format, *((double *)data));
                break;

            // Strings are handled in a special way
            case 's':
                fprintf(printf_fp, format, (char *)data);
                break;

            // % is special
            case '%':
                fprintf(printf_fp, "%%");
                break;

            // Everything else is just printed out as-is
            default:
                fprintf(printf_fp, format);
                break;
        }
        data += CUPRINTF_ALIGN_SIZE;         // Move on to next argument
        *p = c;                     // Restore what we removed
        fmt = p;                    // Adjust fmt string to be past the specifier
        p = strchr(fmt, '%');       // and get the next specifier
    }

    // Print out the last of the string
    fputs(fmt, printf_fp);
    return 1;
}


//
//  doPrintfDisplay
//
//  This runs through the blocks of CUPRINTF_MAX_LEN-sized data, calling the
//  print function above to display them. We've got this separate from
//  cudaPrintfDisplay() below so we can handle the SM_10 architecture
//  partitioning.
//
static int doPrintfDisplay(int headings, int clear, char *bufstart, char *bufend, char *bufptr, char *endptr)
{
    // Grab, piece-by-piece, each output element until we catch
    // up with the circular buffer end pointer
    int printf_count=0;
    char printfbuf_local[CUPRINTF_MAX_LEN+1];
    printfbuf_local[CUPRINTF_MAX_LEN] = '\0';

    while(bufptr != endptr)
    {
        // Wrap ourselves at the end-of-buffer
        if(bufptr == bufend)
            bufptr = bufstart;

        // Adjust our start pointer to within the circular buffer and copy a block.
        cudaMemcpy(printfbuf_local, bufptr, CUPRINTF_MAX_LEN, cudaMemcpyDeviceToHost);

        // If the magic number isn't valid, then this write hasn't gone through
        // yet and we'll wait until it does (or we're past the end for non-async printfs).
        cuPrintfHeader *hdr = (cuPrintfHeader *)printfbuf_local;
        if((hdr->magic != CUPRINTF_SM11_MAGIC) || (hdr->fmtoffset >= CUPRINTF_MAX_LEN))
        {
            //fprintf(printf_fp, "Bad magic number in printf header\n");
            break;
        }

        // Extract all the info and get this printf done
        if(headings)
            fprintf(printf_fp, "[%d, %d]: ", hdr->blockid, hdr->threadid);
        if(hdr->fmtoffset == 0)
            fprintf(printf_fp, "printf buffer overflow\n");
        else if(!outputPrintfData(printfbuf_local+hdr->fmtoffset, printfbuf_local+sizeof(cuPrintfHeader)))
            break;
        printf_count++;

        // Clear if asked
        if(clear)
            cudaMemset(bufptr, 0, CUPRINTF_MAX_LEN);

        // Now advance our start location, because we're done, and keep copying
        bufptr += CUPRINTF_MAX_LEN;
    }

    return printf_count;
}


//
//  cudaPrintfInit
//
//  Takes a buffer length to allocate, creates the memory on the device and
//  returns a pointer to it for when a kernel is called. It's up to the caller
//  to free it.
//
extern "C" cudaError_t cudaPrintfInit(size_t bufferLen)
{
    // Fix up bufferlen to be a multiple of CUPRINTF_MAX_LEN
    bufferLen = (bufferLen < CUPRINTF_MAX_LEN) ? CUPRINTF_MAX_LEN : bufferLen;
    if((bufferLen % CUPRINTF_MAX_LEN) > 0)
        bufferLen += (CUPRINTF_MAX_LEN - (bufferLen % CUPRINTF_MAX_LEN));
    printfbuf_len = (int)bufferLen;

    // Allocate a print buffer on the device and zero it
    if(cudaMalloc((void **)&printfbuf_device, printfbuf_len) != cudaSuccess)
		return cudaErrorInitializationError;
    cudaMemset(printfbuf_device, 0, printfbuf_len);
    printfbuf_start = printfbuf_device;         // Where we start reading from

	// No restrictions to begin with
	cuPrintfRestriction restrict;
	restrict.threadid = restrict.blockid = CUPRINTF_UNRESTRICTED;
	cudaMemcpyToSymbol(restrictRules, &restrict, sizeof(restrict));

    // Initialise the buffer and the respective lengths/pointers.
    cudaMemcpyToSymbol(globalPrintfBuffer, &printfbuf_device, sizeof(char *));
    cudaMemcpyToSymbol(printfBufferPtr, &printfbuf_device, sizeof(char *));
    cudaMemcpyToSymbol(printfBufferLength, &printfbuf_len, sizeof(printfbuf_len));

    return cudaSuccess;
}


//
//  cudaPrintfEnd
//
//  Frees up the memory which we allocated
//
extern "C" void cudaPrintfEnd()
{
    if(!printfbuf_start || !printfbuf_device)
        return;

    cudaFree(printfbuf_device);
    printfbuf_start = printfbuf_device = NULL;
}


//
//  cudaPrintfDisplay
//
//  Each call to this function dumps the entire current contents
//	of the printf buffer to the pre-specified FILE pointer. The
//	circular "start" pointer is advanced so that subsequent calls
//	dumps only new stuff.
//
//  In the case of async memory access (via streams), call this
//  repeatedly to keep trying to empty the buffer. If it's a sync
//  access, then the whole buffer should empty in one go.
//
//	Arguments:
//		outputFP     - File descriptor to output to (NULL => stdout)
//		showThreadID - If true, prints [block,thread] before each line
//
extern "C" cudaError_t cudaPrintfDisplay(void *outputFP, bool showThreadID)
{
	printf_fp = (FILE *)((outputFP == NULL) ? stdout : outputFP);

    // For now, we force "synchronous" mode which means we're not concurrent
	// with kernel execution. This also means we don't need clearOnPrint.
	// If you're patching it for async operation, here's where you want it.
    bool sync_printfs = true;
	bool clearOnPrint = false;

    // Initialisation check
    if(!printfbuf_start || !printfbuf_device || !printf_fp)
        return cudaErrorMissingConfiguration;

    // To determine which architecture we're using, we read the
    // first short from the buffer - it'll be the magic number
    // relating to the version.
    unsigned short magic;
    cudaMemcpy(&magic, printfbuf_device, sizeof(unsigned short), cudaMemcpyDeviceToHost);

    // For SM_10 architecture, we've split our buffer into one-per-thread.
    // That means we must do each thread block separately. It'll require
    // extra reading. We also, for now, don't support async printfs because
    // that requires tracking one start pointer per thread.
    if(magic == CUPRINTF_SM10_MAGIC)
    {
        sync_printfs = true;
	    clearOnPrint = false;
        int blocklen = 0;
        char *blockptr = printfbuf_device;
        while(blockptr < (printfbuf_device + printfbuf_len))
        {
            cuPrintfHeaderSM10 hdr;
            cudaMemcpy(&hdr, blockptr, sizeof(hdr), cudaMemcpyDeviceToHost);

            // We get our block-size-step from the very first header
            if(hdr.thread_buf_len != 0)
                blocklen = hdr.thread_buf_len;

            // No magic number means no printfs from this thread
            if(hdr.magic != CUPRINTF_SM10_MAGIC)
            {
                if(blocklen == 0)
                {
                    fprintf(printf_fp, "No printf headers found at all!\n");
                    break;                              // No valid headers!
                }
                blockptr += blocklen;
                continue;
            }

            // "offset" is non-zero then we can print the block contents
            if(hdr.offset > 0)
            {
                // For synchronous printfs, we must print from endptr->bufend, then from start->end
                if(sync_printfs)
                    doPrintfDisplay(showThreadID, clearOnPrint, blockptr+CUPRINTF_MAX_LEN, blockptr+hdr.thread_buf_len, blockptr+hdr.offset+CUPRINTF_MAX_LEN, blockptr+hdr.thread_buf_len);
                doPrintfDisplay(showThreadID, clearOnPrint, blockptr+CUPRINTF_MAX_LEN, blockptr+hdr.thread_buf_len, blockptr+CUPRINTF_MAX_LEN, blockptr+hdr.offset+CUPRINTF_MAX_LEN);
            }

            // Move on to the next block and loop again
            blockptr += hdr.thread_buf_len;
        }
    }
    // For SM_11 and up, everything is a single buffer and it's simple
    else if(magic == CUPRINTF_SM11_MAGIC)
    {
	    // Grab the current "end of circular buffer" pointer.
        char *printfbuf_end = NULL;
        cudaMemcpyFromSymbol(&printfbuf_end, printfBufferPtr, sizeof(char *));

        // Adjust our starting and ending pointers to within the block
        char *bufptr = ((printfbuf_start - printfbuf_device) % printfbuf_len) + printfbuf_device;
        char *endptr = ((printfbuf_end - printfbuf_device) % printfbuf_len) + printfbuf_device;

        // For synchronous (i.e. after-kernel-exit) printf display, we have to handle circular
        // buffer wrap carefully because we could miss those past "end".
        if(sync_printfs)
            doPrintfDisplay(showThreadID, clearOnPrint, printfbuf_device, printfbuf_device+printfbuf_len, endptr, printfbuf_device+printfbuf_len);
        doPrintfDisplay(showThreadID, clearOnPrint, printfbuf_device, printfbuf_device+printfbuf_len, bufptr, endptr);

        printfbuf_start = printfbuf_end;
    }
    else
        ;//printf("Bad magic number in cuPrintf buffer header\n");

    // If we were synchronous, then we must ensure that the memory is cleared on exit
    // otherwise another kernel launch with a different grid size could conflict.
    if(sync_printfs)
        cudaMemset(printfbuf_device, 0, printfbuf_len);

    return cudaSuccess;
}

// Cleanup
#undef CUPRINTF_MAX_LEN
#undef CUPRINTF_ALIGN_SIZE
#undef CUPRINTF_SM10_MAGIC
#undef CUPRINTF_SM11_MAGIC

#endif

cuPrintf.cuh (also by Nvidia):


/*
	Copyright 2009 NVIDIA Corporation.  All rights reserved.

	NOTICE TO LICENSEE:   

	This source code and/or documentation ("Licensed Deliverables") are subject 
	to NVIDIA intellectual property rights under U.S. and international Copyright 
	laws.  

	These Licensed Deliverables contained herein is PROPRIETARY and CONFIDENTIAL 
	to NVIDIA and is being provided under the terms and conditions of a form of 
	NVIDIA software license agreement by and between NVIDIA and Licensee ("License 
	Agreement") or electronically accepted by Licensee.  Notwithstanding any terms 
	or conditions to the contrary in the License Agreement, reproduction or 
	disclosure of the Licensed Deliverables to any third party without the express 
	written consent of NVIDIA is prohibited.     

	NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE LICENSE AGREEMENT, 
	NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THESE LICENSED 
	DELIVERABLES FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED 
	WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE 
	LICENSED DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, 
	NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.   NOTWITHSTANDING ANY 
	TERMS OR CONDITIONS TO THE CONTRARY IN THE LICENSE AGREEMENT, IN NO EVENT SHALL 
	NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, 
	OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,	WHETHER 
	IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF 
	OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THESE LICENSED DELIVERABLES.  

	U.S. Government End Users. These Licensed Deliverables are a "commercial item" 
	as that term is defined at  48 C.F.R. 2.101 (OCT 1995), consisting  of 
	"commercial computer  software"  and "commercial computer software documentation" 
	as such terms are  used in 48 C.F.R. 12.212 (SEPT 1995) and is provided to the 
	U.S. Government only as a commercial end item.  Consistent with 48 C.F.R.12.212 
	and 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all U.S. Government 
	End Users acquire the Licensed Deliverables with only those rights set forth 
	herein. 

	Any use of the Licensed Deliverables in individual and commercial software must 
	include, in the user documentation and internal comments to the code, the above 
	Disclaimer and U.S. Government End Users Notice.
 */

#ifndef CUPRINTF_H
#define CUPRINTF_H

/*
 *	This is the header file supporting cuPrintf.cu and defining both
 *	the host and device-side interfaces. See that file for some more
 *	explanation and sample use code. See also below for details of the
 *	host-side interfaces.
 *
 *  Quick sample code:
 *
	#include "cuPrintf.cu"
 	
	__global__ void testKernel(int val)
	{
		cuPrintf("Value is: %d\n", val);
	}

	int main()
	{
		cudaPrintfInit();
		testKernel<<< 2, 3 >>>(10);
		cudaPrintfDisplay(stdout, true);
		cudaPrintfEnd();
        return 0;
	}
 */

///////////////////////////////////////////////////////////////////////////////
// DEVICE SIDE
// External function definitions for device-side code

// Abuse of templates to simulate varargs
__device__ int cuPrintf(const char *fmt);
template <typename T1> __device__ int cuPrintf(const char *fmt, T1 arg1);
template <typename T1, typename T2> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2);
template <typename T1, typename T2, typename T3> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3);
template <typename T1, typename T2, typename T3, typename T4> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4);
template <typename T1, typename T2, typename T3, typename T4, typename T5> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6);
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7);
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8);
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9);
template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10> __device__ int cuPrintf(const char *fmt, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10);


//
//	cuPrintfRestrict
//
//	Called to restrict output to a given thread/block. Pass
//	the constant CUPRINTF_UNRESTRICTED to unrestrict output
//	for thread/block IDs. Note you can therefore allow
//	"all printfs from block 3" or "printfs from thread 2
//	on all blocks", or "printfs only from block 1, thread 5".
//
//	Arguments:
//		threadid - Thread ID to allow printfs from
//		blockid - Block ID to allow printfs from
//
//	NOTE: Restrictions last between invocations of
//	kernels unless cudaPrintfInit() is called again.
//
#define CUPRINTF_UNRESTRICTED	-1
__device__ void cuPrintfRestrict(int threadid, int blockid);



///////////////////////////////////////////////////////////////////////////////
// HOST SIDE
// External function definitions for host-side code

//
//	cudaPrintfInit
//
//	Call this once to initialise the printf system. If the output
//	file or buffer size needs to be changed, call cudaPrintfEnd()
//	before re-calling cudaPrintfInit().
//
//	The default size for the buffer is 1 megabyte. For CUDA
//	architecture 1.1 and above, the buffer is filled linearly and
//	is completely used;	however for architecture 1.0, the buffer
//	is divided into as many segments are there are threads, even
//	if some threads do not call cuPrintf().
//
//	Arguments:
//		bufferLen - Length, in bytes, of total space to reserve
//		            (in device global memory) for output.
//
//	Returns:
//		cudaSuccess if all is well.
//
extern "C" cudaError_t cudaPrintfInit(size_t bufferLen=1048576);   // 1-meg - that's enough for 4096 printfs by all threads put together

//
//	cudaPrintfEnd
//
//	Cleans up all memories allocated by cudaPrintfInit().
//	Call this at exit, or before calling cudaPrintfInit() again.
//
extern "C" void cudaPrintfEnd();

//
//	cudaPrintfDisplay
//
//	Dumps the contents of the output buffer to the specified
//	file pointer. If the output pointer is not specified,
//	the default "stdout" is used.
//
//	Arguments:
//		outputFP     - A file pointer to an output stream.
//		showThreadID - If "true", output strings are prefixed
//		               by "[blockid, threadid] " at output.
//
//	Returns:
//		cudaSuccess if all is well.
//
extern "C" cudaError_t cudaPrintfDisplay(void *outputFP=NULL, bool showThreadID=false);

#endif  // CUPRINTF_H

Makefile


HELLOWORLDLIB := libHelloWorld.a

all : $(HELLOWORLDLIB)

CUDA_INSTALL_PATH ?= /usr/local/cuda

NVCC       := $(CUDA_INSTALL_PATH)/bin/nvcc 
CXX	     := g++
ARCHIVER   := ar cqs

TARGETDIR := ../lib
TARGET := $(TARGETDIR)/$(HELLOWORLDLIB)

VERBOSE :=

CUDAINCLUDES  += -I$(CUDA_INSTALL_PATH)/include
COMMONFLAGS += -DUNIX

CXXFLAGS := \
	-W -Wall \
	-Wimplicit \
	-Wswitch \
	-Wformat \
	-Wchar-subscripts \
	-Wparentheses \
	-Wmultichar \
	-Wtrigraphs \
	-Wpointer-arith \
	-Wcast-align \
	-Wreturn-type \
	-Wno-unused-function \
	$(SPACE)
	
NVCCFLAGS := \
	-c -Xopencc \
	-OPT:unroll_size=200000 

# Debug/release configuration
ifeq ($(dbg),1)
	COMMONFLAGS += -g
	NVCCFLAGS   += -D_DEBUG
	CXXFLAGS	+= -D_DEBUG
	CFLAGS    += -D_DEBUG
	OBJDIR   := debug
	LIBSUFFIX   := D
else 
	COMMONFLAGS += -O2 
	OBJDIR   := release
	LIBSUFFIX   :=
	NVCCFLAGS   += --compiler-options -fno-strict-aliasing
	CXXFLAGS	+= -fno-strict-aliasing
	CFLAGS    += -fno-strict-aliasing
endif
	
CUDALIB := -L$(CUDA_INSTALL_PATH)/lib
CUDALIB += -lcudart -lcutil

# Add common flags
NVCCFLAGS += $(COMMONFLAGS) $(COMMONINCLUDES) $(CUDAINCLUDES)
CFLAGS  += $(COMMONFLAGS) $(COMMONINCLUDES) 
CXXFLAGS  += $(COMMONFLAGS) $(COMMONINCLUDES) 

CUDAOBJS := \
	$(OBJDIR)/HelloWorld.cu.o 

$(HELLOWORLDLIB): directories $(CUDAOBJS)
	$(ARCHIVER) $(TARGET) $(CUDAOBJS)

$(OBJDIR)/HelloWorld.cu.o : HelloWorld.cu $(CU_DEPS)
	$(VERBOSE)$(NVCC) $(NVCCFLAGS) -I. -o $(OBJDIR)/HelloWorld.cu.o -c HelloWorld.cu

directories:
	$(VERBOSE)mkdir -p $(OBJDIR)
	$(VERBOSE)mkdir -p $(TARGETDIR)

clean:
	$(VERBOSE)rm -r $(OBJDIR)
	$(VERBOSE)rm -r $(TARGET)

Be sure to change any relevant paths in the makefile. I save all of these files in a folder called cuda/src/, then if I issue:


cd cuda/src
make

I build the static library cuda/lib/libHelloWorld.a

Building a qt app

My qt app consists of four files located in the same directory as the cuda subdirectory mentioned above:

HelloWorldQt.pro


INCLUDEPATH += cuda/src
        
CUDA_LIBDIR = /usr/local/cuda/lib
CUDALIB = -L$$CUDA_LIBDIR -lcudart

SOURCES += main.cpp \
	   HelloButton.cpp
HEADERS += HelloButton.h

LIBS += -Lcuda/lib -lHelloWorld $$CUDALIB

HelloButton.cpp


#include <HelloWorld.cuh>
#include <HelloButton.h>

HelloButton::HelloButton(const QString & text, QWidget * parent) : 
  QPushButton(text, parent)
{
}

void HelloButton::on_clicked()
{
  HelloWorld();
}

HelloButton.h


#include <QPushButton>
class HelloButton : public QPushButton
{
  Q_OBJECT
  public:
  public:
    HelloButton(const QString & text, QWidget * parent = 0);
    virtual ~HelloButton(){};
  public slots:
    void on_clicked();
};

main.cpp


#include <QApplication>
#include <HelloButton.h>
int main(int argc, char * argv[])
{
  QApplication app(argc, argv);
  HelloButton hello_button("Hello, GPU!");
  QObject::connect(
    &hello_button,
    SIGNAL(clicked()), 
    &hello_button, 
    SLOT(on_clicked()));
  hello_button.show();
  return app.exec();
}

Now you can generate an Xcode project using Qmake:


qmake-mac -spec macx-xcode HelloWorldQt.pro

Building and running with Xcode

There’s some trickiness getting executables to run correctly when linking to cuda libraries. If you just build and run the project generate by the above you may see errors like:


dyld: Library not loaded: @rpath/libcudart.dylib
  Referenced from: /Users/ajx/Code/Cuda/HelloWorldQt/build/Debug/HelloWorldQt.app/Contents/MacOS/HelloWorldQt
  Reason: image not found

There are a few ways to fix this. I prefer this simple one, but the down side is that the final app must be run from Xcode.

Then open HelloWorldQt.xcodeproj, in the side bar open Executables > Right click on HelloWorldQt and select Get Info. Then click the Arguments tab, add a new variable “to be set in the environment”:
Name: DYLD_LIBRARY_PATH
Value: /usr/local/lib/cuda

This will let you build and run from Xcode, to run the app NOT via Xcode you will have to do fancy stuff with otool that I’m not bothering with as of yet.

Download project tree source code

Universal File Dialog, a first step

Tuesday, August 31st, 2010

I’m switching from Qt to straight Glut with AntTweakBar for my next project. I’m happy about getting rid of Qt. For my small prototyping projects it was too heavy (hard to get a target working on computers that don’t have Qt installed) and too much work to add simple ui for tweaking parameters and flipping flags. AntTweakBar doesn’t yet solve the deployable problem (I’m trying to figure that out too) but is miles better for prototyping. The only thing I miss from Qt is the native File Save/File Open Dialogs. Since Glut is fairly universal I have taken a stab at writing a small header that could be the start of a universal file dialog “library”. I’d only want this library to depend on standard includes and it only needs to implement get_file_save_path and get_file_open_path.

Here’s what I put in FileDialog.h:


#include <stdio.h>
#define FILE_DIALOG_MAX_BUFFER 1024

// Sets buffer to a path to an existing file 
// buffer[0]=0 on cancel
//
// Usage:
//   char buffer[FILE_DIALOG_MAX_BUFFER];
//   get_open_file_path(buffer);
void get_open_file_path(char buffer[]){
#ifdef __APPLE__
  // For apple use applescript hack
  FILE * output = popen(
    "osascript -e \""
    "   tell application \\\"System Events\\\"\n"
    "           activate\n"
    "           set existing_file to choose file\n"
    "   end tell\n"
    "   set existing_file_path to (POSIX path of (existing_file))\n"
    "\" 2>/dev/null | tr -d '\n' ","r");
  while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
  }
#else
  // For every other machine type 
  printf("Please enter a file path: ");
  gets(buffer);
#endif
}

// Sets buffer to a path to a new/existing file 
// buffer[0]=0 on cancel
//
// Usage:
//   char buffer[FILE_DIALOG_MAX_BUFFER];
//   get_save_file_path(buffer);
void get_save_file_path(char buffer[]){
#ifdef __APPLE__
  // For apple use applescript hack
  // There is currently a bug in Applescript that strips extensions off
  // of chosen existing files in the "choose file name" dialog
  // I'm assuming that will be fixed soon :-) 
  FILE * output = popen(
    "osascript -e \""
    "   tell application \\\"System Events\\\"\n"
    "           activate\n"
    "           set existing_file to choose file name\n"
    "   end tell\n"
    "   set existing_file_path to (POSIX path of (existing_file))\n"
    "\" 2>/dev/null | tr -d '\n' ","r");
  while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
  }
#else
  // For every other machine type 
  printf("Please enter a file path: ");
  gets(buffer);
#endif
}

And here’s a sample program that calls both functions. Save it in test.c:


#include "FileDialog.h"
#include <stdio.h>

int main(void){
  char buffer[FILE_DIALOG_MAX_BUFFER];

  get_open_file_path(buffer);
  if(buffer[0] == 0)
    printf("Cancelled\n");
  else
    printf("Open file path: %s\n",buffer);

  get_save_file_path(buffer);
  if(buffer[0] == 0)
    printf("Cancelled\n");
  else
    printf("Save file path: %s\n",buffer);

  return 0;
};

Compile and run on a Mac with:


gcc -o test test.c;
./test

Update: I've been looking into a way to do the above for Mac using Carbon or Cocoa but it seems impossible to do correctly without starting up an entire new app...

Update: I've noticed many annoying quirks about the above applescript. There's no way to specify only showing certain file extensions (just UTI types but that's not always what you want). It's quite slow to pop up. But most annoying was that focus was not returned to my GLUT app. Here's the hack I place in my app to use applescript to return the focus to my app. Seems to work great:


// you call to FileDialog.h
get_open_file_path(...)
#ifdef __APPLE__
  // Hack to put focus back on app
  FILE * output = popen(                            
    "osascript -e \""
    "   tell application \\\"NameOfYourApp\\\"\n"           
    "           activate\n"
    "   end tell\"\n"                               
    ,"r");                                          
#endif

Of course, replace NameOfYourApp with the name of your app. If you're unsure what this name is, try using the name that appears above your app's icon on the dock when you hover over it.

QString to char *

Wednesday, July 14th, 2010

I’ve been writing a lot in C++ lately and I am always so frustrated working with strings. Scripting languages like Ruby and Python make it so easy. Even Java/C# are a cinch. My latest problem has come from the handy Qt functions: getSaveFileName(…) and getOpenFileName(…). These were surprisingly easy to just drop in to my code. The only problem is that they return a QString. That’s the thing about working in C++, everybody has to reinvent the string and you end up with a bagillion implementations that all have finicky details.

I already had a function like this:


void write_data(char * file_name);

So what I needed was a way to turn the QString returned by getSaveFileName(…) into a char * so that I could pass it to my method. Here’s how I did it:


QString file_name = QFileDialog::getSaveFileName(
  this,
  "Save data",
  "~",
  "Data (*.dat)");
if(obj_file_name.length() == 0){
  // Cancel button clicked in qt dialog
}else{
  write_data(file_name.toAscii().data());  
}

Any reason that I might run into trouble with the .toAscii() part?

Vim syntax highlighting for Qt’s .pro files

Tuesday, July 13th, 2010

I use the following at the end of my .vimrc file to force vim to highlight Qt’s .pro project files like a make file:


au BufNewFile,BufRead *.pro set filetype=make

Looks much better than whatever it was trying to do before.

The trials and tribulations of building a simple mesh viewer

Friday, February 26th, 2010

I am beginning a new project and to start I needed to install a peer’s CGAL-based mesh viewer. The mesh viewer has many dependencies which in turn have even more dependencies. I will try to recap my struggle and eventual success, here. My warned that this is not an installation guide and is probably missing many things I did and maybe even advertising bad solutions.

64-bit on Mac OS X 10.5

Short story: Don’t bother.

Long story: My peer has his whole setup all running on 64 bits. This requires building all the dependencies running on 64 bits, which for Mac OS X 10.4 (Leopard) this means compiling everything from source and being really careful about flags. It also means, as far as I know, not using macports.
I tried to mimic his compilations and installs but without knowing the exact flags I got almost to the point of having everything working but then my 32-bit installs from macports started really getting in the way. I ended up uninstalling all my macports in vain only to realize that my python(s) were not 64-bit or at least couldn’t find 32-bit framework libraries. I recompiled python and pyqt but only found new errors. In the end I gave up on 64-bit (which I’d spent 3 days on) and switched (back) to 32-bit (which only took 1 day).

Dependencies for CGAL using macports

CGAL itself has a macport but it’s poorly maintained and will often not compile correctly with your current setup. It seems that a lot of people are having trouble getting this to even build correctly much less work correctly. Instead use macports to install the dependencies then build cgal from source by hand (not so bad).

CGAL depends on or supports the following libraries that you don’t already have on your mac:

  • boost
  • Gmp
  • Mpfr
  • zlib
  • BLAS*
  • LAPACK*
  • taucs*

Qt3, Qt4 and libqglviewer are only necessary for building the examples and demos which require them and not necessary for the install. Really the only dependency is boost. The rest add more features: exact arithmetic, linear algebra solvers, etc. Those with a *, I’ll write more about later on.

To install most of the above with macports just issue:


sudo port install boost gmp mpfr zlib

Now, you should be ready to build CGAL.

Installing CGAL

cd to the CGAL directory and issue

cmake -i .

You’ll see a prompt for advanced options, I was paranoid so I typed Yes:


Would you like to see advanced options? [No]:Yes

You need to change all library and include directories to point to the macports install directory: /opt/local/lib/WHATEVER or /opt/local/include.

For example:


Variable Name: Boost_INCLUDE_DIR
Description: Path to a file.
Current Value: /usr/local/include
New Value (Enter to keep current value): /opt/local/include

Do this for boost, gmp, mpfr etc. Watch out for the CMAKE_INSTALL_PREFIX prompt. If you want CGAL to hang out with your macports then change this to /opt/local like so:


Variable Name: CMAKE_INSTALL_PREFIX
Description: Install path prefix, prepended onto install directories.
Current Value: /usr/local
New Value (Enter to keep current value): /opt/local

In the end, my hackish solution to a later problem prevents all this /opt/local business from mattering but it seems like good practice to have related software in the same place. Unless you feel like macports should be the only one touching /opt/local/ stuff, but it's your computer so why not.

Then you can make, sudo make install to finish with CGAL.

Python 2.6 and PyQt4

Simply issue:


sudo port install python26 py26-pyqt4 py26-opengl
sudo python_select python26

These should install fine and work on a simple example. Getting CGAL to recognize your python (and subsequently PyQt) is another problem.

taucs with LU

I was trying to install and unsupported version of the math software taucs. This version had LU decomposition necessary for a feature of the mesh viewer. I ran into trouble forcing the taucs build to see my blas and lapack and to make a proper 32 bit file.

After running configure if I tried to run make I would see an error:


build/darwin9.0/makefile:14: config/darwin9.0.mk: No such file or directory
make: *** No rule to make target `config/darwin9.0.mk'.  Stop.

This is because the taucs only cam with a premade make file for darwin, not darwin9.0 as configure has recognized my OS.

For the most part you can just copy the darwin make file:


cp config/darwin.mk config/darwin9.0.mk

But make a few changes, namely edit the following lines:


CFLAGS    = -arch i386 -O3 -faltivec

LIBBLAS   = -framework Accelerate
LIBLAPACK = -framework Accelerate

One more thing. The LIBF77 = -Lexternal/lib/darwin -lf2c line is pointing to the right place but the file there is wrong. If you ar -x external/lib/darwin/libf2c.a you'll find out it's full of x86_64 .o files, which will lead to a confused architecture build. My solution was to download the taucs_full from the CGAL download page and copy the libf2c.a on top of this file. Maybe the same is true for the lbmetis.a, I don't remember. Just check that after you
make your libtaus.a unarchives to i386 mach-o files.

After running make you should move your taucs to the /opt/local area. I just did


sudo mkdir /opt/local/taucs_with_lu
sudo cp -r taucs_with_lu/* /opt/local/taucs_with_lu/.

Hacks

At this point things seemed to work but our meshviewer uses swig to combine cpp code and python. swig was easy enough to install:


sudo port install swig swig-python

But CGAL was not playing nicely. When I built our CGAL meshviewer, CGAL's cmake finders were not locating the libraries I had installed with macports. It kept trying to look in /usr/local/. My hack was to move /usr/local to /usr/local-off and simple link it to /opt/local. Everyone I told this too agreed it was ugly.


sudo mv /usr/local /usr/local-off
sudo ln -s /opt/local /usr/local

This is basically saying, I agree to use macports for everything or I must be very careful.

At this point CGAL would play along and build the toy version of our meshviewer. But python would not display it. I think it was because swig would not read the headers from the right place (/opt/local/WHATEVER) instead it was looking in


/Library/Frameworks/Python.Framework/WHATEVER. To handle this I also hacked:
cd /Library/Frameworks/Python.framework/Versions
ln -s /opt/local/Library/Frameworks/Python.framework/Versions/Current Current
cd /Library/Frameworks/Python.framework/
mv Headers Headers-off
ln -s /opt/local/Library/Frameworks/Python.framework/Headers Headers

Again, everyone I told this too said it was a poor man's hack.

CGAL and a deprecated header

Finally, I had everything in order to build the full version of my meshviewer. But I got funny errors about a certain boost header no existing, namely /opt/local/include/boost/property_map.hpp.

Upon inspection I noticed that indeed this file does not exist. On my peer's boost install (he did not use macports) he had this file but opening it we saw that it had been long deprecated by boost and simple pointed to the real header in property_map/property_map.hpp one directory lower. I copied my peer's deprecated property_map.hpp and put it in /opt/local/include/boost/ and CGAL found the file correctly.

Everything is up and running currently with the exception that my LAPACK is complaining more than my peer's about instantiating primitives correctly. When I solve this (hopefully not with another hack) I will post the results.

Hope this helps somebody. Please let me know if you find non-hack solutions to any of these.

Update:
I think I figured out Blas and Lapack...

In my cmake CONFIG.cmake file I have


    SET(CMAKE_CXX_FLAGS "-arch i386")
    SET(CMAKE_C_FLAGS   "-arch i386")
    SET(BLAS_LIBRARIES "/System/Library/Frameworks/Accelerate.framework")
    SET(BLAS_DEFINITIONS "-DBLAS_USE_F2C")
    INCLUDE_DIRECTORIES("/opt/local/include")
    LINK_DIRECTORIES("/opt/local/lib" "lib")
    INCLUDE_DIRECTORIES(/usr/X11/include)
    LINK_DIRECTORIES(/usr/X11/lib)
    SET(TAUCS_INCLUDE_DIR "/opt/local/taucs_with_lu/src/" "/opt/local/taucs_with
    SET(TAUCS_LIBRARIES "/opt/local/taucs_with_lu/lib/darwin9.0/libtaucs.a")
    SET(METIS_LIBRARIES "/opt/local/lib/libmetis.a")

In some files it worked to just above the line where you include the Acceleration framework


#include 

put instead:


#define __LP64__
#include 

For other directories I added this line to the CMakeLists.txt file:


ADD_DEFINITIONS("-D__LP64__")

If I find an elegant way to do this I will repost and update...

PyQt’s magical disappearing (but not reappearing) windows on Mac OS X

Monday, November 16th, 2009

After the grueling Qt and PyQt installation process on my Mac OS X 10.5 machine, I have PyQt up and running. I made a little hello, window type app for a sanity test and found a very puzzling feature. PyQt can make a resizable window which the mouse can resize to nothing! This is not allowed through the API methods (namely widget.resize(...,...)), with those the window always keeps a minimum width and height. But the user is allowed to drag the window to nothingness. If the user lets go while the window has non-positive dimensions then the window is lost forever as far as the mouse user is concerned.

Here’s the simple PyQt code:


import sys
from PyQt4 import QtGui

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.show()
sys.exit(app.exec_())

And here are some screenshots of the result:

pyqt-disappearing-window-01.png


pyqt-disappearing-window-02.png


pyqt-disappearing-window-03.png


pyqt-disappearing-window-04.png


pyqt-disappearing-window-05.png


pyqt-disappearing-window-06.png


pyqt-disappearing-window-07.png


pyqt-disappearing-window-08.png

Update:
Here’s an animated gif version illustrating the above:
pyqt disappearing window animated gif

Update:
Shucks! Seems like this is fixed in the new version of PyQt4 that I just got.

Yet another post about installing qtruby on mac

Wednesday, October 7th, 2009

Trying to install qtruby for mac os x 10.5 on my own (following their instructions) I got an error during cmake, like


i686-apple-darwin9-g++-4.0.1: /Users/Zack/Desktop/qt4-qtruby-2.0.3/smoke/qtwebkit/x_1.cpp: No such file or directory
i686-apple-darwin9-g++-4.0.1: no input files
make[2]: *** [smoke/qtwebkit/CMakeFiles/smokeqtwebkit.dir/x_1.o] Error 1
make[1]: *** [smoke/qtwebkit/CMakeFiles/smokeqtwebkit.dir/all] Error 2
make: *** [all] Error 2

I followed the instructions on another blog and my cmake finished correctly and make and sudo make install compiled and installed smoothly…BUT!

When I went into irb, and issued


require 'qt4'

I got this exciting error:


/opt/local/lib/ruby/site_ruby/1.8/i686-darwin9/qtruby4.bundle: [BUG] Bus Error
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin9]

Abort trap

I found a solution on a rubyforge help site, which suggested more gobbedygook to add to my cmake call. I add these flags to the flags from before giving me a final cmake line that looked like this:


cmake -DENABLE_QTWEBKIT_SMOKE=off -DENABLE_QTWEBKIT_RUBY=off -DENABLE_QTTEST=off -DENABLE_QTTEST_SMOKE=off -DCMAKE_BUILD_TYPE=debugfull -DRUBY_INCLUDE_PATH=/opt/local/lib/ruby/1.8/i686-darwin9 -DRUBY_LIBRARY=/opt/local/lib/libruby.dylib .

This worked! After make and sudo make install I can go into irb and issue require 'qt4' with no errors.

Here’s a screen shot of my test in irb:
qtruby test app in irb