PRINTFFUNCTION(3clc)
β¨ PRINTF FUNCTION (3clc) β¨
okay bestie, welcome to the OpenCL Manual π
this is the 3clc page and we're talking about the printf function, and no cap it's a whole journey. grab a strawberry π and let's go.
NAME π
printf_Function β the OpenCL C programming language implements the printf function. that's it, that's the vibe. she's iconic, she's built-in, she prints things.
int printf(constant char * restrict format, ...);
DESCRIPTION π¦
so the printf built-in function writes output to an implementation-defined stream (think like stdout, but it's up to the implementation to decide the details, iykyk). it does this under the control of the string pointed to by format, which is the little instruction manual telling it how the subsequent arguments get converted for output.
here's where it gets spicy though:
- if there are insufficient arguments for the format, the behavior is undefined. π like, no promises, do not do this.
- if the format is exhausted while arguments remain, the excess arguments are evaluated (as always β they still get evaluated, periodt) but are otherwise ignored.
- the
printffunction returns when the end of the format string is encountered.
NOTES πΈ
return value check β¨
printf returns 0 if it was executed successfully and -1 otherwise.
printf output synchronization π
okay so the timing of when your output actually shows up? here's the tea:
- when the event that is associated with a particular kernel invocation is completed, the output of all
printf()calls executed by this kernel invocation is flushed to the implementation-defined output stream. - calling
clFinish(3clc)on a command queue flushes all pending output byprintfin previously enqueued and completed commands to the implementation-defined output stream. - in the case that
printfis executed from multiple work-items concurrently, there is no guarantee of ordering with respect to written data. for example, it is valid for the output of a work-item with a global id(0,0,1)to appear intermixed with the output of a work-item with a global id(0,0,4)and so on. like, they're just gonna braid together bestie, that's the deal π
printf format string π
the format shall be a character sequence, beginning and ending in its initial shift state. the format is composed of zero or more directives:
- ordinary characters (not
%), which are copied unchanged to the output stream; and - conversion specifications, each of which results in fetching zero or more subsequent arguments, converting them, if applicable, according to the corresponding conversion specifier, and then writing the result to the output stream.
and here's the important part: since format is in the constant address space, it must be resolvable at compile time and thus cannot be dynamically created by the executing program, itself. so no building your format string at runtime, bestie, it's giving compile-time-only energy. π
each conversion specification is introduced by the character %. after the %, the following appear in sequence:
- zero or more flags (in any order) that modify the meaning of the conversion specification.
- an optional minimum field width. if the converted value has fewer characters than the field width, it is padded with spaces (by default) on the left (or right, if the left adjustment flag, described later, has been given) to the field width. the field width takes the form of a nonnegative decimal integer. note that
0is taken as a flag, not as the beginning of a field width. - an optional precision that gives the minimum number of digits to appear for the
d,i,o,u,x, andXconversions, the number of digits to appear after the decimal point character fora,A,e,E,f, andFconversions, the maximum number of significant digits for thegandGconversions, or the maximum number of bytes to be written forsconversions. the precision takes the form of a period (.) followed by an optional decimal integer; if only the period is specified, the precision is taken as zero. if a precision appears with any other conversion specifier, the behavior is undefined. - an optional vector specifier.
- a length modifier that specifies the size of the argument. the length modifier is required with a vector specifier and together specifies the vector type. implicit conversions between vector types are disallowed (as per section 6.2.1). if the vector specifier is not specified, the length modifier is optional.
- a conversion specifier character that specifies the type of conversion to be applied.
the flag characters and their meanings π
-β the result of the conversion is left-justified within the field. (it is right-justified if this flag is not specified.)+β the result of a signed conversion always begins with a plus or minus sign. (it begins with a sign only when a negative value is converted if this flag is not specified.) the results of all floating conversions of a negative zero, and of negative values that round to zero, include a minus sign.spaceβ if the first character of a signed conversion is not a sign, or if a signed conversion results in no characters, a space is prefixed to the result. if the space and+flags both appear, the space flag is ignored.#β the result is converted to an "alternative form". foroconversion, it increases the precision, if and only if necessary, to force the first digit of the result to be a zero (if the value and precision are both 0, a single0is printed). forx(orX) conversion, a nonzero result has0x(or0X) prefixed to it. fora,A,e,E,f,F,g, andGconversions, the result of converting a floating-point number always contains a decimal-point character, even if no digits follow it. (normally, a decimal-point character appears in the result of these conversions only if a digit follows it.) forgandGconversions, trailing zeros are not removed from the result. for other conversions, the behavior is undefined.0β ford,i,o,u,x,X,a,A,e,E,f,F,g, andGconversions, leading zeros (following any indication of sign or base) are used to pad to the field width rather than performing space padding, except when converting an infinity or NaN. if the0and-flags both appear, the0flag is ignored. ford,i,o,u,x, andXconversions, if a precision is specified, the0flag is ignored. for other conversions, the behavior is undefined.
the vector specifier and its meaning π§
vnβ specifies that a followinga,A,e,E,f,F,g,G,d,i,o,u,x, orXconversion specifier applies to a vector argument, wherenis the size of the vector and must be 2, 3, 4, 8 or 16. the vector value is displayed in the following general form:
value1 C value2 C ... C valuen
where C is a separator character. the value for this separator character is a comma.
and that's part 1 bestie, we're not done yet, more coming π«Άπ
okay so if the vector specifier is not used, the length modifiers and their meanings are:
hhβ says a followingd,i,o,u,x, orXconversion specifier applies to acharorucharargument (the arg will have been promoted according to the integer promotions, but its value shall be converted tocharorucharbefore printing). πhβ says a followingd,i,o,u,x, orXconversion specifier applies to ashortorushortargument (the arg will have been promoted according to the integer promotions, but its value shall be converted toshortorunsigned shortbefore printing). β¨l (ell)β says a followingd,i,o,u,x, orXconversion specifier applies to alongorulongargument. Thelmodifier is supported by the full profile. For the embedded profile, thelmodifier is supported only if 64-bit integers are supported by the device. π«Ά
If the vector specifier is used, the length modifiers and their meanings are:
hhβ says a followingd,i,o,u,x, orXconversion specifier applies to acharnorucharnargument (the arg will not be promoted). πhβ says a followingd,i,o,u,x, orXconversion specifier applies to ashortnorushortnargument (the arg will not be promoted); that a followinga,A,e,E,f,F,g, orGconversion specifier applies to ahalfnargument if thecl_khr_fp16(3clc)extension is supported. πΈhlβ this modifier can only be used with the vector specifier. Specifies that a followingd,i,o,u,x, orXconversion specifier applies to aintnoruintnargument; that a followinga,A,e,E,f,F,g, orGconversion specifier applies to afloatnargument. πl (ell)β specifies that a followingd,i,o,u,x, orXconversion specifier applies to alongnorulongnargument; that a followinga,A,e,E,f,F,g, orGconversion specifier applies to adoublenargument. Thelmodifier is supported by the full profile. For the embedded profile, thelmodifier is supported only if 64-bit integers or double-precision floating-point are supported by the device. π
If a vector specifier shows up without a length modifier, the behavior is undefined. The vector data type described by the vector specifier and length modifier must match the data type of the argument; otherwise the behavior is undefined.
If a length modifier appears with any conversion specifier other than as specified above, the behavior is undefined
okay so the conversion specifiers and their meanings are literally this:
d,iβ Theint,charn,shortn,intnorlongnargument is converted to signed decimal in the style[β]dddd. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is1. The result of converting a zero value with a precision of zero is no characters. periodt πo,u,x,Xβ Theunsigned int,ucharn,ushortn,uintnorulongnargument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal notation (xorX) in the styledddd; the lettersabcdefare used forxconversion and the lettersABCDEFforXconversion. The precision specifies the minimum number of digits to appear; if the value being converted can be represented in fewer digits, it is expanded with leading zeros. The default precision is1. The result of converting a zero value with a precision of zero is no characters. slay β¨f,Fβ Adouble,halfn,floatnordoublenargument representing a floating-point number is converted to decimal notation in the style[β]ddd.ddd, where the number of digits after the decimal-point character is equal to the precision specification. If the precision is missing, it is taken as6; if the precision is zero and the#flag is not specified, no decimal-point character appears. If a decimal-point character appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. Adouble,halfn,floatnordoublenargument representing an infinity is converted in one of the styles[-]infor[-]infinityβ which style is implementation-defined. Adouble,halfn,floatnordoublenargument representing a NaN is converted in one of the styles[- ]nanor[-]nan(n-char-sequence)β which style, and the meaning of anyn-charsequence, is implementation-defined. TheFconversion specifier producesINF,INFINITY, orNANinstead ofinf,infinity, ornan, respectively. When applied to infinite and NaN values, the-,+, and space flag characters have their usual meaning; the#and0flag characters have no effect. obsessed π¦e,Eβ Adouble,halfn,floatnordoublenargument representing a floating-point number is converted in the style[β]d.ddd eΒ±dd, where there is one digit (which is nonzero if the argument is nonzero) before the decimal-point character and the number of digits after it is equal to the precision; if the precision is missing, it is taken as6; if the precision is zero and the#flag is not specified, no decimal-point character appears. The value is rounded to the appropriate number of digits. TheEconversion specifier produces a number withEinstead ofeintroducing the exponent. The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent. If the value is zero, the exponent is zero. Adouble,halfn,floatnordoublenargument representing an infinity or NaN is converted in the style of anforFconversion specifier. lowkey iconic π₯g,Gβ Adouble,halfn,floatnordoublenargument representing a floating-point number is converted in stylefore(or in styleForEin the case of aGconversion specifier), depending on the value converted and the precision. LetPequal the precision if nonzero,6if the precision is omitted, or1if the precision is zero. Then, if a conversion with styleEwould have an exponent ofX: β ifP > X β₯ β4, the conversion is with stylef(orF) and precisionP β (X + 1). β otherwise, the conversion is with stylee(orE) and precisionP β 1. Finally, unless the#flag is used, any trailing zeros are removed from the fractional portion of the result and the decimal-point character is removed if there is no fractional portion remaining. Adouble,halfn,floatnordoublen eargument representing an infinity or NaN is converted in the style of anforFconversion specifier. no cap πa,Aβ Adouble,halfn,floatnordoublenargument representing a floating-point number is converted in the style[β]0xh.hhhh pΒ±d, where there is one hexadecimal digit (which is nonzero if the argument is a normalized floating-point number and is otherwise unspecified) before the decimal-point character) and the number of hexadecimal digits after it is equal to the precision; if the precision is missing, then the precision is sufficient for an exact representation of the value; if the precision is zero and the#flag is not specified, no decimal point character appears. The lettersabcdefare used foraconversion and the lettersABCDEFforAconversion. TheAconversion specifier produces a number withXandPinstead ofxandp. The exponent always contains at least one digit, and only as many more digits as necessary to represent the decimal exponent of2. If the value is zero, the exponent is zero. Adouble,halfn,floatnordoublenargument
A value representing an infinity or NaN is converted in the style of an f or F conversion specifier. Binary implementations can choose the hexadecimal digit to the left of the decimal-point character so that subsequent digits align to nibble (4-bit) boundaries. π
NOTE: The conversion specifierse,E,g,G,a,Aconvert afloatorhalfargument that is a scalar type to adoubleonly if thedoubledata type is supported. If thedoubledata type is not supported, the argument will be afloatinstead of adoubleand thehalftype will be converted to afloat.
cβ Theintargument is converted to anunsigned char, and the resulting character is written.sβ The argument shall be a literal string. No special provisions are made for multibyte characters. The behavior ofprintfwith thesconversion specifier is undefined if the argument value is not a pointer to a literal string. Characters from the literal string array are written up to (but not including) the terminating null character. If the precision is specified, no more than that many bytes are written. If the precision is not specified or is greater than the size of the array, the array shall contain a null character.pβ The argument shall be a pointer tovoid. The pointer can refer to a memory region in the global, constant, local or private address space. The value of the pointer is converted to a sequence of printing characters in an implementation-defined manner.%β A%character is written. No argument is converted. The complete conversion specification shall be%%.
If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
In no case does a nonexistent or small field width cause truncation of a field; if the result of a conversion is wider than the field width, the field is expanded to contain the conversion result.
For a and A conversions, the value is correctly rounded to a hexadecimal floating number with the given precision.
π Differences between OpenCL C and C99 printf π
- The
lmodifier followed by acconversion specifier orsconversion specifier is not supported by OpenCL C. - The
ll,j,z,t, andLlength modifiers are not supported by OpenCL C. - The
nconversion specifier is not supported by OpenCL C but is reserved. - OpenCL C adds the optional
vnvector specifier to support printing of vector types. - The conversion specifiers
f,F,e,E,g,G,a,Aconvert afloatargument to adoubleonly if thedoubledata type is supported. Refer to the description ofCL_DEVICE_DOUBLE_FP_CONFIG. If thedoubledata type is not supported, the argument will be afloatinstead of adouble. - For the embedded profile, the
llength modifier is supported only if 64-bit integers are supported. - In OpenCL C,
printfreturns0if it was executed successfully and-1otherwise vs. C99 whereprintfreturns the number of characters printed or a negative value if an output or encoding error occurred. - In OpenCL C, the conversion specifier
scan only be used for arguments that are literal strings.
β¨ Examples β¨
float4 f = (float4)(1.0f, 2.0f, 3.0f,
4.0f); uchar4 uc = (uchar4)(0xFA, 0xFB,
0xFC, 0xFD);
printf("f4 = %2.2v4hlf\n", f);
printf("uc = %#v4hhx\n", uc);
// The above two calls print the
following:
f4 = 1.00,2.00,3.00,4.00 uc =
0xfa,0xfb,0xfc,0xfd
A few examples of valid use cases of printf for the conversion specifier s are given below. The argument value must be a pointer to a literal string.
kernel void my_kernel( ... ) {
printf("%s\n", "this is a test
string\n"); }
A few examples of invalid use cases of printf for the conversion specifier s are given below:
kernel void my_kernel(global char *s,
... ) { printf("%s\n", s);
constant char *p = "this is a test
string\n"; printf("%s\n", p);
printf("%s\n", &p[3]); }
A few examples of invalid use cases of printf where data types given by the vector specifier and length modifier do not match the argument type are given below:
kernel void my_kernel(global char *s,
... ) { uint2 ui = (uint2)(0x12345678,
0x87654321); printf("unsigned short
value = (%#v2hx)\n", ui)
printf("unsigned char value =
(%#v2hhx)\n", ui) }
π¦ SPECIFICATION π¦
OpenCL Specification[1]
π AUTHORS π
The Khronos Group
π COPYRIGHT π
Copyright Β© 2007-2011 The Khronos Group Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.
πΈ NOTES πΈ
- OpenCL Specification page 284, section 6.12.13 - printf
That's a wrap, bestie! π This iconic page comes from The Khronos Group, dated 01/14/2021, and it's the PRINTF FUNCTION(3clc) manual. Stay slaying! β¨π
π¦ where this page comes from
- ubuntu jammy: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- ubuntu noble: opencl-1.2-man-doc 1.0~svn33624-5 Β· other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms