http://x.cygwin.com/
Showing posts with label c. Show all posts
Showing posts with label c. Show all posts
Friday, November 27, 2009
Thursday, November 19, 2009
Graphviz
Graphviz is a tool used to draw graphs.
Monday, August 31, 2009
Creating blocks and grids in CUDA
GPU's are capable of performing task that are performed by CPU's, CUDA was developed.
This program demonstrates how to create grids and block in a process.
#include stdio.h
#include cuda.h
// Kernel that executes on the CUDA device
__global__ void square_array()
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
printf("idx %d blockIdx.x %d blockDim.x %d threadIdx.x %d\n",idx,blockIdx.x,blockDim.x,threadIdx.x);
}
// main routine that executes on the host
int main(void)
{
int N=9; // length of an array
int block_size = 4; // number of threads that fit in a block
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1); // number of blocks
square_array <<<>>> ();
}
if you execute the program you will get the following output:
xxxxx@hpcc:~/prog$ ./test
idx 0 blockIdx.x 0 blockDim.x 4 threadIdx.x 0
idx 1 blockIdx.x 0 blockDim.x 4 threadIdx.x 1
idx 2 blockIdx.x 0 blockDim.x 4 threadIdx.x 2
idx 3 blockIdx.x 0 blockDim.x 4 threadIdx.x 3
idx 4 blockIdx.x 1 blockDim.x 4 threadIdx.x 0
idx 5 blockIdx.x 1 blockDim.x 4 threadIdx.x 1
idx 6 blockIdx.x 1 blockDim.x 4 threadIdx.x 2
idx 7 blockIdx.x 1 blockDim.x 4 threadIdx.x 3
idx 8 blockIdx.x 2 blockDim.x 4 threadIdx.x 0
idx 9 blockIdx.x 2 blockDim.x 4 threadIdx.x 1
idx 10 blockIdx.x 2 blockDim.x 4 threadIdx.x 2
idx 11 blockIdx.x 2 blockDim.x 4 threadIdx.x 3
int block_size = 4;(it is blockDim.x, each block contain 4 threads)
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
this instruction will generate n_blocks=3 (number of blocks, in above output it is blockIdx.x)
How to compile:
Write program and save it with ".cu" extension.
$xyz.cu
setup environment variables
$set up LD_LIBRARY_PATH
$export LD_LIBRARY_PATH=$PATH:/home/cuda/lib/
compile
$/home/cuda/bin/nvcc -deviceemu xyz.cu -o xyz
run
$./xyz
This program demonstrates how to create grids and block in a process.
#include stdio.h
#include cuda.h
// Kernel that executes on the CUDA device
__global__ void square_array()
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
printf("idx %d blockIdx.x %d blockDim.x %d threadIdx.x %d\n",idx,blockIdx.x,blockDim.x,threadIdx.x);
}
// main routine that executes on the host
int main(void)
{
int N=9; // length of an array
int block_size = 4; // number of threads that fit in a block
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1); // number of blocks
square_array <<<>>> ();
}
if you execute the program you will get the following output:
xxxxx@hpcc:~/prog$ ./test
idx 0 blockIdx.x 0 blockDim.x 4 threadIdx.x 0
idx 1 blockIdx.x 0 blockDim.x 4 threadIdx.x 1
idx 2 blockIdx.x 0 blockDim.x 4 threadIdx.x 2
idx 3 blockIdx.x 0 blockDim.x 4 threadIdx.x 3
idx 4 blockIdx.x 1 blockDim.x 4 threadIdx.x 0
idx 5 blockIdx.x 1 blockDim.x 4 threadIdx.x 1
idx 6 blockIdx.x 1 blockDim.x 4 threadIdx.x 2
idx 7 blockIdx.x 1 blockDim.x 4 threadIdx.x 3
idx 8 blockIdx.x 2 blockDim.x 4 threadIdx.x 0
idx 9 blockIdx.x 2 blockDim.x 4 threadIdx.x 1
idx 10 blockIdx.x 2 blockDim.x 4 threadIdx.x 2
idx 11 blockIdx.x 2 blockDim.x 4 threadIdx.x 3
int block_size = 4;(it is blockDim.x, each block contain 4 threads)
int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
this instruction will generate n_blocks=3 (number of blocks, in above output it is blockIdx.x)
How to compile:
Write program and save it with ".cu" extension.
$xyz.cu
setup environment variables
$set up LD_LIBRARY_PATH
$export LD_LIBRARY_PATH=$PATH:/home/cuda/lib/
compile
$/home/cuda/bin/nvcc -deviceemu xyz.cu -o xyz
run
$./xyz
Thursday, May 28, 2009
Basic socket programming (TCP)
Sockets are end points on a machine for communication.
Below code demonstatres connection oriented program (TCP).
Client:
#include following libraries stdio.h, sys/socket.h, sys/types.h, netinet/in.h, netdb.h
int main(int argc,char **argv)
{
int sockfd,newsockfd; // socket descriptors
struct sockaddr_in serv_addr;
struct hostent *he;
char a[50],a1[50];
sockfd=socket(AF_INET,SOCK_STREAM,0); // creates a socket
if(sockfd<0)
{
printf("Socket failed");
exit(0);
}
// Use either green or yellow code, if you want to give server name as command line argument use the green one.
if((he=gethostbyname(argv[2]))==NULL) // gets the ip address
{
printf("gethost error");
exit(1);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.127.127.1");
memcpy(&(serv_addr.sin_addr),he->h_addr,he->h_length);
serv_addr.sin_port=htons(atoi(argv[1]));
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\nConnection failed");
exit(0);
}
printf("\nEnter a Message");
scanf("%s",&a);
write(sockfd,a,50);
read(sockfd,a1,50);
printf("\nClient received Message %s",a1);
close(sockfd); // closing the socket descriptor
}
Server:
#include following libraries stdio.h, sys/socket.h, sys/types.h, netinet/in.h
int main(int argc,char **argv)
{
int sockfd,newsockfd,clilen;
struct sockaddr_in serv_addr,cli_addr;
char a[50];
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("\nSocket failed!");
exit(0);
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(atoi(argv[1]));
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\nBind failed");
exit(1);
}
if(listen(sockfd,5)<0)
{
printf("\nListen fails");
exit(0);
}
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
read(newsockfd,a,80);
printf("Server received %s",a);
write(newsockfd,"Server received message",26);
close(newsockfd);
close(sockfd);
}
compilation:
client : gcc -o client client.c
server : gcc -o server server.c
Execution:
server : ./server (portno)
client : ./client (portno) (servername)
Communication between client and server.
Client side:
client creates a socket and sends a request to server
Server side:
- Server creates a socket and waiting for a request.
- when request arrives to server socket then it creates a new socket and forward the request to that new socket.
- Further communication is goes on in between client's socket and server's new socket.
Monday, May 11, 2009
Different ways of reading a file in C
In C there are different methods to read a file, but which is more suitable to your code is important. Here we look into different functions and see how it reads from a file.
file.txt (Input)
I am in your blogspot.
fscanf:
FILE *stream,*fopen();
stream=fopen("file.txt","r");
while(!feof(stream))
{
fscanf(stream,"%s",x);
printf("%s\n",x);
}
output:
I
am
in
your
blogspot.
blogspot.
It is clear that is taking string by string and printing on the screen. Have you notice blogspot. is printing twice. we can avoid that using memcpy command.
#include
main()
{
FILE *stream,*fopen();
char x[100];
stream=fopen("file.txt","r");
while(!feof(stream))
{
fscanf(stream,"%s",x);
printf("%s\n",x);
memcpy(x,"\0",100); // every time it clears x
}
}
Output:
I
am
in
your
blogspot.
fgets:
while(fgets(x,5, stream) != NULL)
{
printf("%s\n",x);
}
Output:
I am
in
your
blo
gspo
t.
Every time, fgets method takes 5 character from the stream and puts into user buffer.
fgetc:
x=fgetc(stream);
while(x!=EOF)
{
printf("%c\n",x);
x=fgetc(stream);
}
Output:
I
a
m
i
n
y
o
u
r
b
l
o
g
s
p
o
t
.
it reads character by character from file.
Friday, March 6, 2009
Remote Procedure Call
RPC (Remote Procedure Call), it is higher level programming to socket programming.
.x files are just like Interface in java.
conversion.x
struct conv{
float x;
};
program conversion{
version conversion1_vers{
int conversion1(conv)=1;
}=1;
version conversion2_vers{
int converions2(conv)=1;
}=2;
}= 0x23451111;
In Conversion.x, conv is a structures used to send a float value.
To generate client and server code use rpcgen
rpcgen -a -C conversion.x
along with client and server, it will also generate makefile.conversion and someother files.
conversion_client.c
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "conversion.h"
#include
#include /* getenv, exit */
void
conversion_1(float a,char *host)
{
CLIENT *clnt;
int *result_1;
conv conversion1_1_arg;
#ifndef DEBUG
clnt = clnt_create(host, conversion, conversion1_vers, "netpath");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
conversion1_1_arg.x=a;
result_1 = conversion1_1(&conversion1_1_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror(clnt, "call failed");
}
else
{
printf("%.2f Kilometers is equal to %d Miles\n",a,*result_1);
}
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
}
void
conversion_2(float a,char *host)
{
CLIENT *clnt;
int *result_1;
conv converions2_2_arg;
#ifndef DEBUG
clnt = clnt_create(host, conversion, conversion2_vers, "netpath");
if (clnt == (CLIENT *) NULL) {
clnt_pcreateerror(host);
exit(1);
}
#endif /* DEBUG */
converions2_2_arg.x=a;
result_1 = converions2_2(&converions2_2_arg, clnt);
if (result_1 == (int *) NULL) {
clnt_perror(clnt, "call failed");
}
else
{
printf("%.2f Kilometers is equal to %d Feet\n",a,*result_1);
}
#ifndef DEBUG
clnt_destroy(clnt);
#endif /* DEBUG */
}
main(int argc, char *argv[])
{
char *host;
float a;
if (argc <>
printf("usage: %s server_host\n", argv[0]);
exit(1);
}
host = argv[1];
//According to your logic you need to write your code, here i put "a" as a parameter in both conversion_1 and conversion_2 inorder to send to server.
printf("Enter kilometers\n");
scanf("%f",&a);
conversion_1(a,host);
conversion_2(a,host);
}
conversion_server.c
/*
* This is sample code generated by rpcgen.
* These are only templates and you can use them
* as a guideline for developing your own functions.
*/
#include "conversion.h"
#include
#include /* getenv, exit */
#include
#include
int *
//After generating this code, we need to write our definition to this methods.
conversion1_1_svc(conv *argp, struct svc_req *rqstp)
{
static int result;
/*
* insert server code here
*/
result=(int)floorf((argp->x/1.61)+0.5);
printf("1st resutl is %d\n",result);
return (&result);
}
int *
converions2_2_svc(conv *argp, struct svc_req *rqstp)
{
static int result;
/*
* insert server code here
*/
result=(int)floorf(((argp->x) * 3280.839895)+0.5);
printf("2nd result is %d\n",result);
return (&result);
}
Change the make.conversion to makefile.
mv makefile.conversion makefile
in makefile you need to change the following.
CC=gcc // default is cc
CFLAGS += -g -DRPC_SVC_FG
RPCGENFLAGS = -C
To compile
$ make
Open two command prompts, one for server and other for client.
$./conversion_server
$./conversion_client
Subscribe to:
Posts (Atom)
