How to get a bit field size using a template? - c++11

I'm using a macro to compute the size of a bit-field. Would it be possible to use a template?
Code example:
#include <stdio.h>
/*!
#param reg a bit-fields structure
#param field name of one of the fields in 'reg'
*/
#define GET_FIELD_MAX(reg, field) ({ \
decltype(reg) r; \
r.field = ~0UL; \
r.field; })
struct A {
unsigned a : 3;
unsigned b : 4;
};
int main()
{
A x;
printf("b size = %d", GET_FIELD_MAX(x, b));
}
Output:
b size = 15

Related

error: no matching function for call to 'swap'

I am trying to sort cakeTypes vector by the size of their weight. But getting the error in sort implementation.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class CakeType
{
public:
const unsigned int weight_;
const unsigned int value_;
CakeType(unsigned int weight = 0, unsigned int value = 0) :
weight_(weight),
value_(value)
{}
};
bool compareCakes(const CakeType& cake1, const CakeType& cake2) {
return cake1.weight_ < cake2.weight_;
}
unsigned long long maxDuffelBagValue(const std::vector<CakeType>& cakeTypes,
unsigned int weightCapacity)
{
// calculate the maximum value that we can carry
unsigned cakeTypesSize = cakeTypes.size();
unsigned long long valueCalculator[weightCapacity+1][cakeTypesSize+1];
for (unsigned int i = 0; i<=weightCapacity+1; i++) {
valueCalculator[i][0] = 0;
}
for (unsigned int i = 0; i<=cakeTypesSize+1; i++) {
valueCalculator[0][i] = 0;
}
vector<CakeType> sortedCakeTypes(cakeTypes);
sort(sortedCakeTypes.begin(), sortedCakeTypes.end(), compareCakes);
return 0;
}
This is part of there error:
exited with non-zero code (1).
In file included from solution.cc:1:
In file included from /usr/include/c++/v1/iostream:38:
In file included from /usr/include/c++/v1/ios:216:
In file included from /usr/include/c++/v1/__locale:15:
In file included from /usr/include/c++/v1/string:439:
/usr/include/c++/v1/algorithm:3856:17: error: no matching function for call to 'swap'
swap(*__first, *__last);
^~~~
I tried this solution sort() - No matching function for call to 'swap', but it is not the same issue.
Data type which is used by swap function in sort algorithm must be MoveAssignable, then you can perform operation like below
CakeType c1, c2;
c1 = move(c2); // <- move c2 to c1
But in your case CakeType has const data members. You can assign values to const data members only in constructors. Code cannot be compiled because default move/copy assignment operator can't be generated by this restriction (assignment to const member is illegal).
Remove const specifier from your class definition and code will work.
class CakeType
{
public:
unsigned int weight_;
unsigned int value_;
CakeType(unsigned int weight = 0, unsigned int value = 0) :
weight_(weight),
value_(value)
{}
};

Use CUSP matrix inside CUDA function? [duplicate]

This question already has an answer here:
How to get raw pointer from cusp library matrix format
(1 answer)
Closed 4 years ago.
i want to write a kernel function that takes as input 2 CUSP matrices A and B,then fills data into B in parallel.
#include <cusp/coo_matrix.h>
#include <cusp/print.h>
#include <iostream>
__global__ void kernel_example(cusp::coo_matrix<int,float,cusp::host_memory>* A,
cusp::coo_matrix<int,float,cusp::host_memory>* B){
printf("hello from kernel...");
//actual operations go here.
}
int main(void)
{
// allocate storage
cusp::coo_matrix<int,float,cusp::host_memory> A(4,3,6);
cusp::coo_matrix<int,float,cusp::host_memory> B(4,3,6);
// initialize matrix entries on host
A.row_indices[0] = 0; A.column_indices[0] = 0; A.values[0] = 10;
A.row_indices[1] = 0; A.column_indices[1] = 2; A.values[1] = 20;
A.row_indices[2] = 2; A.column_indices[2] = 2; A.values[2] = 30;
A.row_indices[3] = 3; A.column_indices[3] = 0; A.values[3] = 40;
A.row_indices[4] = 3; A.column_indices[4] = 1; A.values[4] = 50;
A.row_indices[5] = 3; A.column_indices[5] = 2; A.values[5] = 60;
kernel_example<<<1,1>>>(A,B);
cudaDeviceSynchronize();
return 0;
}
the following error ensues:
error: no suitable conversion function from "cusp::coo_matrix<int, float, cusp::host_memory>" to "cusp::coo_matrix<int, float, cusp::host_memory> *" exists
how do i go about it?
The error is because the function signature is for a pointer, and you're passing an object. You can pass by reference and it will build.
Should be
__global__ void kernel_example(cusp::coo_matrix<int, float, cusp::host_memory>& A,
cusp::coo_matrix<int, float, cusp::host_memory>& B) {
printf("hello from kernel...");
//actual operations go here.
}

Is there a memset-like function which can set integer value in visual studio?

1, It is a pity that memset(void* dst, int value, size_t size) fools a lot of people when they first use this function! 2nd parameter "int value" should be "uchar value" to describe the real operation inside.
Don't misunderstand me, I am asking a memset-like function!
2, I know there are some c++ candy function like std::fill_n(my_array, array_length, constant_value);
even a pure c function in OS X: memset_pattern4(grid, &pattern, sizeof grid);
mentioned in a perfect thread Why is memset() incorrectly initializing int?.
So, is there a similar c function in runtime library of visual studio like memset_pattern4()?
3, for somebody asked why i wouldn't use a for-loop to set integer by integer. here is my answer: memset turns to a better performance when setting big trunk(10K?) of memory at least in x86.
http://www.gamedev.net/topic/472631-performance-of-memset/page-2 gives more discussion, although without a conclusion(I doubt there will be).
4, said function can be used to simplify counting sort by avoiding useless Fibonacci accumulation.
Original:
for (int i = 0; i < SRC_ARRY_SIZE; i++)
counter_arry[src_arry[i]]++;
for (int i = SRC_LOW_BOUND; i < SRC_HI_BOUND; i++)//forward fabnacci??
counter_arry[i+1] += counter_arry[i];
for (int i = 0; i < SRC_ARRY_SIZE; i++)
{
value = src_arry[i];
map = --counter_arry[value];//then counter down!
temp[map] = value;
}
Expected:
for (int i = 0; i < SRC_ARRY_SIZE; i++)
counter_arry[src_arry[i]]++;
for (int i = SRC_LOW_BOUND; i < SRC_HI_BOUND+1; i++)//forward fabnacci??
{
memset_4(cur_ptr,i, counter_arry[i]);
cur_ptr += counter_arry[i];
}
Thanks for your kindly review and reply!
Here's an implementation of memset_pattern4() that you might find useful. It's nothing like Darwin's SSE assembly language version, except that it has the same interface.
#include <string.h>
#include <stdint.h>
/*
A portable implementation of the Darwin memset_patternX() family of functions:
These are analogous to memset(), except that they fill memory with a replicated
pattern either 4, 8, or 16 bytes long. b points to a buffer of size len bytes
which is to be filled. The second parameter points to the pattern. If the
buffer length is not an even multiple of the pattern length, the last instance
of the pattern will be truncated. Neither the buffer nor the pattern pointer
need be aligned.
*/
/*
alignment utility macros stolen from Linux
see https://lkml.org/lkml/2006/11/25/2 for a discussion of why typeof() is used
*/
#if !_MSC_VER
#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((uintptr_t)(p), (a)))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
#define IS_PTR_ALIGNED(p, a) (IS_ALIGNED((uintptr_t)(p), (a)))
#else
/* MS friendly versions */
/* taken from the DDK's fltKernel.h header */
#define IS_ALIGNED(_pointer, _alignment) \
((((uintptr_t) (_pointer)) & ((_alignment) - 1)) == 0)
#define ROUND_TO_SIZE(_length, _alignment) \
((((uintptr_t)(_length)) + ((_alignment)-1)) & ~(uintptr_t) ((_alignment) - 1))
#define __ALIGN_KERNEL(x, a) ROUND_TO_SIZE( (x), (a))
#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
#define PTR_ALIGN(p, a) ALIGN((p), (a))
#define IS_PTR_ALIGNED(p, a) (IS_ALIGNED((uintptr_t)(p), (a)))
#endif
void nx_memset_pattern4(void *b, const void *pattern4, size_t len)
{
enum { pattern_len = 4 };
unsigned char* dst = (unsigned char*) b;
unsigned const char* src = (unsigned const char*) pattern4;
if (IS_PTR_ALIGNED( dst, pattern_len) && IS_PTR_ALIGNED( src, pattern_len)) {
/* handle aligned moves */
uint32_t val = *((uint32_t*)src);
uint32_t* word_dst = (uint32_t*) dst;
size_t word_count = len / pattern_len;
dst += word_count * pattern_len;
len -= word_count * pattern_len;
for (; word_count != 0; --word_count) {
*word_dst++ = val;
}
}
else {
while (pattern_len <= len) {
memcpy(dst, src, pattern_len);
dst += pattern_len;
len -= pattern_len;
}
}
memcpy( dst, src, len);
}

Postgres fulltext search with near operator LIKE oracle context index

In Oracle, I can search in a Clob with query like "NEAR((a,b,c), 5)". This is documentation from oracle:
Use the NEAR operator to return a score based on the proximity of two or more query terms. Oracle Text returns higher scores for terms closer together and lower scores for terms farther apart in a document.
How can I do that in Postgres? I just need an index that could search the word nearby another word.
Here is a Hamming Distance function http://en.wikipedia.org/wiki/Hamming_distance
Header file
/**
#file HammingDistance.h A C header file to compute the Hamming Distance between two strings
as PostgreSQl C User Defined Function
*/
#ifndef HAMMINGDISTANCE_H_INCLUDED
#define HAMMINGDISTANCE_H_INCLUDED
DLLEXPORT Datum DistanceCstring(PG_FUNCTION_ARGS);
DLLEXPORT Datum Distance(PG_FUNCTION_ARGS);
#endif // HAMMINGDISTANCE_H_INCLUDED
Source file
/**
#file HammingDistance.c A C source file to compute the Hamming Distance between two strings
as PostgreSQl C User Defined Function
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "postgres.h"
#include "utils/geo_decls.h"
#include "utils/builtins.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#define VC_EXTRALEAN
#pragma warning (disable : 4996)
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#ifdef _WIN32
#define DLLEXPORT _declspsec(dllexport)
#else
#define DLLEXPORT
#endif // _WIN32
static int32_t DistanceString( unsigned char * a, int32_t la , unsigned char * b, int32_t lb);
static int32_t DistanceUChar( unsigned char a, unsigned char b);
PG_FUNCTION_INFO_V1(Distance);
/**
DistanceCstring An SQL function to Compute Hamming Distance between two text types
#param[in] A a Text type;
#param[in] B a text type
#return The hamming Distance between two Text types
**/
DLLEXPORT Datum Distance(PG_FUNCTION_ARGS)
{
text * a = PG_GETARG_TEXT_PP(0);
text * b = PG_GETARG_TEXT_PP(1);
unsigned char * ac;
unsigned char * bc;
int32_t distance = 0;
ac = text_to_cstring( a );
bc = text_to_cstring( b );
distance = DistanceString( ac, strlen(ac), bc, strlen(bc) );
PG_RETURN_INT32(distance);
}
PG_FUNCTION_INFO_V1(DistanceCstring);
/**
DistanceCstring An SQL function to Compute Hamming Distance between two strings
#param[in] A a Cstring type
#param[in] B a Cstring type
#return The hamming Distance between two Cstring types
**/
DLLEXPORT Datum DistanceCstring(PG_FUNCTION_ARGS)
{
unsigned char * ac = (unsigned char *) PG_GETARG_CSTRING(0);
unsigned char * bc = (unsigned char *) PG_GETARG_CSTRING(1);
int32_t distance = 0;
distance = DistanceString( ac, strlen(ac), bc, strlen(bc) );
PG_RETURN_INT32(distance);
}
/**
DistanceString Compute Hamming Distance between two unsigned char strings
#param[in] a an unsigned char array
#param[in] la length of a in char
#param[in] b an unsigned char array
#param[in] lb length of b in char
#return Hamming distance
**/
static int32_t DistanceString( unsigned char * a, int32_t la , unsigned char * b, int32_t lb)
{
unsigned char * smaller;
unsigned char * larger;
int i = 0;
int length = 0;
int32_t distance = 0;
int delta = 0;
if ( lb > la )
{
delta = lb - la;
length = la;
smaller = a;
larger = b;
}
else
{
delta = la - lb;
length = lb;
smaller = b;
larger = a;
}
for( i = 0; i < length; i++ )
{
distance += DistanceUChar( * smaller++, * larger++);
}
for( i = 0; i < delta ; i++ )
{
distance += DistanceUChar( 0, * larger++);
}
return distance;
}
/**
DistanceUChar Compute Hamming Distance between two unsigned chars
#param[in] a unsigned char
#param[in] b unsigned char
#return Hamming Distance between two unsigned chars
**/
static int32_t DistanceUChar( unsigned char a, unsigned char b)
{
unsigned char x = a ^ b;
int32_t distance = 0;
if ( (x & 0x1 )== 0x1 )
distance++;
if ( (x & 0x2) == 0x2 )
distance++;
if ( (x & 0x4) == 0x4 )
distance++;
if ( (x & 0x8) == 0x8 )
distance++;
if ( x & 0x10 == 0x10 )
distance++;
if ( (x & 0x20) == 0x20 )
distance++;
if ( (x & 0x40) == 0x40 )
distance++;
if ( (x & 0x80) == 0x80 )
distance++;
return distance;
}
Makefile
OPTS := -g -fpic -c -I /opt/PostgreSQL/9.1/include/postgresql/server
INSTALLDIR := /opt/PostgreSQL/9.1/lib/postgresql
all: HammingDistance.so
HammingDistance.so: HammingDistance.c HammingDistance.h
gcc HammingDistance.c $(OPTS) -o HammingDistance.o
gcc -shared -o HammingDistance.so HammingDistance.o
clean:
rm -rf *.o *.so
register:
psql -f install.sql -p 5433 -U postgres -d postgres ;
install:
sudo cp HammingDistance.so $(INSTALLDIR);
test:
psql -f Test.sql -p 5433 -U postgres -d postgres ;
Install SQL
-- define the schema
set search_path to public;
-- Remove existing function
drop function if exists Distance( text, text ) cascade;
drop function if exists Distance( cstring , cstring ) cascade;
-- Create the new one
create or replace function Distance( text, text ) returns integer
as '$libdir/HammingDistance', 'Distance'
language c strict;
create or replace function Distance( cstring, cstring ) returns integer
as '$libdir/HammingDistance', 'DistanceCstring'
language c strict

Boost.Variant Vs Virtual Interface Performance

I'm trying to measure a performance difference between using Boost.Variant and using virtual interfaces. For example, suppose I want to increment different types of numbers uniformly, using Boost.Variant I would use a boost::variant over int and float and a static visitor which increments each one of them. Using class interfaces I would use a pure virtual class number and number_int and number_float classes which derive from it and implement an "increment" method.
From my testing, using interfaces is far faster than using Boost.Variant.
I ran the code at the bottom and received these results:
Virtual: 00:00:00.001028
Variant: 00:00:00.012081
Why do you suppose this difference is? I thought Boost.Variant would be a lot faster.
** Note: Usually Boost.Variant uses heap allocations to guarantee that the variant would always be non-empty. But I read on the Boost.Variant documentation that if boost::has_nothrow_copy is true then it doesn't use heap allocations which should make things significantly faster. For int and float boost::has_nothrow_copy is true.
Here is my code for measuring the two approaches against each other.
#include <iostream>
#include <boost/variant/variant.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/format.hpp>
const int iterations_count = 100000;
// a visitor that increments a variant by N
template <int N>
struct add : boost::static_visitor<> {
template <typename T>
void operator() (T& t) const {
t += N;
}
};
// a number interface
struct number {
virtual void increment() = 0;
};
// number interface implementation for all types
template <typename T>
struct number_ : number {
number_(T t = 0) : t(t) {}
virtual void increment() {
t += 1;
}
T t;
};
void use_virtual() {
number_<int> num_int;
number* num = &num_int;
for (int i = 0; i < iterations_count; i++) {
num->increment();
}
}
void use_variant() {
typedef boost::variant<int, float, double> number;
number num = 0;
for (int i = 0; i < iterations_count; i++) {
boost::apply_visitor(add<1>(), num);
}
}
int main() {
using namespace boost::posix_time;
ptime start, end;
time_duration d1, d2;
// virtual
start = microsec_clock::universal_time();
use_virtual();
end = microsec_clock::universal_time();
// store result
d1 = end - start;
// variant
start = microsec_clock::universal_time();
use_variant();
end = microsec_clock::universal_time();
// store result
d2 = end - start;
// output
std::cout <<
boost::format(
"Virtual: %1%\n"
"Variant: %2%\n"
) % d1 % d2;
}
For those interested, after I was a bit frustrated, I passed the option -O2 to the compiler and boost::variant was way faster than a virtual call.
Thanks
This is obvious that -O2 reduces the variant time, because that whole loop is optimized away. Change the implementation to return the accumulated result to the caller, so that the optimizer wouldn't remove the loop, and you'll get the real difference:
Output:
Virtual: 00:00:00.000120 = 10000000
Variant: 00:00:00.013483 = 10000000
#include <iostream>
#include <boost/variant/variant.hpp>
#include <boost/variant/static_visitor.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
#include <boost/format.hpp>
const int iterations_count = 100000000;
// a visitor that increments a variant by N
template <int N>
struct add : boost::static_visitor<> {
template <typename T>
void operator() (T& t) const {
t += N;
}
};
// a visitor that increments a variant by N
template <typename T, typename V>
T get(const V& v) {
struct getter : boost::static_visitor<T> {
T operator() (T t) const { return t; }
};
return boost::apply_visitor(getter(), v);
}
// a number interface
struct number {
virtual void increment() = 0;
};
// number interface implementation for all types
template <typename T>
struct number_ : number {
number_(T t = 0) : t(t) {}
virtual void increment() { t += 1; }
T t;
};
int use_virtual() {
number_<int> num_int;
number* num = &num_int;
for (int i = 0; i < iterations_count; i++) {
num->increment();
}
return num_int.t;
}
int use_variant() {
typedef boost::variant<int, float, double> number;
number num = 0;
for (int i = 0; i < iterations_count; i++) {
boost::apply_visitor(add<1>(), num);
}
return get<int>(num);
}
int main() {
using namespace boost::posix_time;
ptime start, end;
time_duration d1, d2;
// virtual
start = microsec_clock::universal_time();
int i1 = use_virtual();
end = microsec_clock::universal_time();
// store result
d1 = end - start;
// variant
start = microsec_clock::universal_time();
int i2 = use_variant();
end = microsec_clock::universal_time();
// store result
d2 = end - start;
// output
std::cout <<
boost::format(
"Virtual: %1% = %2%\n"
"Variant: %3% = %4%\n"
) % d1 % i1 % d2 % i2;
}

Resources