This snippet of code reads in a bit of unformatted, double precision data, and returns a real FFTW on it:
#include <stdio.h>
#include <fftw3.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int i, fdin;
int count=128;
static fftw_plan fplan=0;
double alldata[2][count];
fdin = open("rand_walk.dat", O_RDONLY);
read(fdin, alldata[0], sizeof(double)*count);
fplan = fftw_plan_r2r_1d(count, alldata[0],
alldata[1], FFTW_R2HC, FFTW_ESTIMATE);
fftw_execute_r2r(fplan, alldata[0], alldata[1]);
printf("%lf\n",alldata[0][0]);
printf("%lf\n",alldata[1][10]);
return 0;
}
It works just fine when compiled under GNU or Cray. When compiled under Intel, it runs without any error message, but the output array (alldata[1])is filled with zeros. Apparently, there is something about FFTW3 with Intel that I don't understand. Anyone know what that might be? BTW, switching to FFTW2 is not an option on the platform I have to work on.
I have tried with
#include <stdio.h>
#include <fftw3.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int i, fdin;
int count=2;
static fftw_plan fplan=0;
double alldata[2][2]={1,1,1,1};
fplan = fftw_plan_r2r_1d(count, alldata[0],
alldata[1], FFTW_R2HC, FFTW_ESTIMATE);
fftw_execute_r2r(fplan, alldata[0], alldata[1]);
printf("%lf\n",alldata[0][0]);
printf("%lf\n",alldata[1][0]);
return 0;
}
and compiled with the command:
icpc -mkl -I"${MKLROOT}/include/fftw" sample.cpp and got 1.0 and 1.0. as the result.
Related
I am making a game and I have to add some sounds effects and Music.
I Googled it and I found The flowing Code:
#include <conio.h>
#include "inc/fmod.h"
FSOUND_SAMPLE* handle;
int main ()
{
// init FMOD sound system
FSOUND_Init (44100, 32, 0);
// load and play mp3
handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
FSOUND_PlaySound (0,handle);
// wait until the users hits a key to end the app
while (!_kbhit())
{
}
// clean up
FSOUND_Sample_Free (handle);
FSOUND_Close();
}
But when I compile it I got the flowing error:
➜ Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
^~~~~~~~~
1 error generated.
Well, firstly <conio.h> is a C++ library and you're programming in C. It's different!
Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}
Then, this is the other source file, called file.c:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
void scala1(unsigned char x){
if(x=='a')beep(131,50);
if(x=='s')beep(147,50);
if(x=='d')beep(165,50);
if(x=='f')beep(175,50);
if(x=='g')beep(196,50);
if(x=='h')beep(220,50);
if(x=='j')beep(247,50);
if(x=='k')beep(262,50);
if(x=='l')beep(294,50);
if(x=='w')beep(139,50);
if(x=='e')beep(156,50);
if(x=='r')beep(185,50);
if(x=='t')beep(208,50);
if(x=='y')beep(233,50);
}
void scala2(unsigned char x){
if(x=='a')beep(262,50);
if(x=='s')beep(294,50);
if(x=='d')beep(330,50);
if(x=='f')beep(349,50);
if(x=='g')beep(392,50);
if(x=='h')beep(440,50);
if(x=='j')beep(494,50);
if(x=='k')beep(523,50);
if(x=='l')beep(587,50);
if(x=='w')beep(277,50);
if(x=='e')beep(311,50);
if(x=='r')beep(370,50);
if(x=='t')beep(415,50);
if(x=='y')beep(466,50);
}
void scala3(unsigned char x){
if(x=='a')beep(523,50);
if(x=='s')beep(587,50);
if(x=='d')beep(659,50);
if(x=='f')beep(698,50);
if(x=='g')beep(784,50);
if(x=='h')beep(880,50);
if(x=='j')beep(988,50);
if(x=='k')beep(1046,50);
if(x=='l')beep(1175,50);
if(x=='w')beep(554,50);
if(x=='e')beep(622,50);
if(x=='r')beep(740,50);
if(x=='t')beep(831,50);
if(x=='y')beep(932,50);
}
The last one, the file header.h. It's code is the following one:
void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);
All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)
I tried many guides and for some reason, my syscall isnt implementing correctly. ERRNO is 38 (function not implemented). Here are my steps to creating a basic one for me as a beginner:
I am doing this on a raspberry pi 64 bit. with linux 4.14.93
inside "/linux/" folder:
I created a folder called "mycall" with files mycall.c, mycall.h, & Makefile
mycall.c:
#include <linux/kernel.h>
#include <linux/init.h>
#include <sched.h>
#include <syscalls.h>
#include <linux/unistd.h>
#include "mycall.h"
asmlinkage long sys_mysyscall(int *id, int username, int *size)
{
printk("hello");
return 0;
}
mycall.h:
asmlinkage long sys_mysyscall(int *id, int username, int *size);
Makefile:
obj-y := mycall.o
then I went into all of the places where I THOUGHT i should declare the syscall.
// inside of /linux/include/linux/syscalls.h
asmlinkage long sys_mysyscall(int __user *myid, int username, int __user *size);
then to
// inside of /linux/arch/arm/tools/syscall.tbl
398 common mysyscall sys_mysyscall
finally i added
// inside of /linux/Makefile
core-y += kernel/ certs/ mm/ fs/ ipc/ security/ crypt/ block/ mycall/
thats my set up. then the final thing I do is make a userspace in /linux directory
userspace.c:
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <pwd.h>
int main ()
{
int id = 0;
int username = 7;
int size = 2;
int ret_val = syscall(398, &id, username, &size);
printf("%d\n", ret_val);
return 0;
}
// recompiling & copying . then I run the userspace again
cd ~/linux
KERNEL=kernel7
make bcm2709_defconfig
make -j4 zImage modules dtbs
sudo make INSTALL_MOD_PATH=/root modules_install
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm/boot/zImage /boot/$KERNEL.img
the return value is -1 so this doesn't work.
please let me know what I'm doing wrong. I have read several implementation guides and have no idea how I'm implementing this wrong.
I want to implement RSA-Signature where I can calculate the signature of a file and verify a signature of a file. Therefore I use the libary gmp.
But when I want to print the data of the file, it always prints 0 even though the file is not empty. Here is my code:
//compiled with gcc rsa-sig.c -O3 -Wall -l gmp -o rsa-sig
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <gmp.h> /* GNU Multi Precision library */
int main(int argc, char *argv[])
{
int r;
mpz_t modulus;
mpz_init(modulus);
r=mpz_set_str(modulus, "FFFF",16); //just an example modulus
mpz_t data;
mpz_init(data);
FILE * stream;
stream = fopen("file.bin", "rb");
r= mpz_inp_str(data, stream,16);
mpz_mod(data,data,modulus);
gmp_printf("%Zd\n",data);
}
I can't figure out why the output of that is 0. Maybe one of you guys have an idea.
Thanks!!
The following MEXed C code simply makes calls to magma to invert a matrix. The stand alone C code (which is also posted) works, but the mex code crashes.
I've triple checked the documentation, verified that other magma functions work as expected, and posted on the Magma forum and was told my code is fine (this post is a cross post from Magma forum). This means that the problem is with mex. I would like to know what is causing the mex code to seg-fault and how to get it to run as expected.
Mexed code:
#include <mex.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stddef.h>
#include <magma_v2.h>
#include <cuda_runtime.h>
void mat2magma(magmaDoubleComplex* p, double* pr, double* pi,int numElements)
{
int j=0;
for(j=0;j<numElements;j++){
p[j].x=pr[j];
p[j].y=pi[j];
}
}
void magma2mat(magmaDoubleComplex* p, double* pr, double* pi,int numElements)
{
int j=0;
for(j=0;j<numElements;j++){
pr[j]= p[j].x;
pi[j]= p[j].y;
}
}
/*gateway function*/
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
/*initialize magma*/
magma_init();
magma_queue_t queue = NULL;
magma_device_t dev;
magma_getdevice(&dev);
magma_queue_create(dev,&queue );
magma_int_t m,ldwork,info;
magma_int_t *piv;
magmaDoubleComplex *a,*da,*dwork;
/* Matlab -> Host */
m=mxGetM(prhs[0]);
piv=(magma_int_t*) malloc(m*sizeof(magma_int_t));
magma_zmalloc_cpu(&a,m*m);
mat2magma(a,mxGetPr(prhs[0]),mxGetPi(prhs[0]),m*m);
ldwork = m*magma_get_zgetri_nb(m);
/* Host -> GPU */
magma_zmalloc(&dwork,ldwork);
magma_zmalloc(&da,m*m);
magma_zsetmatrix(m,m,a,m,da,m,queue);
/*LU and Inverse */
magma_zgetrf_gpu(m,m,da,m,piv,&info);
magma_zgetri_gpu(m,da,m,piv,dwork,ldwork,&info);
/*GPU -> Host */
magma_zgetmatrix(m,m,da,m,a,m,queue);
/*Host -> Matlab*/
plhs[0] = mxCreateDoubleMatrix(m,m,mxCOMPLEX);
magma2mat(a,mxGetPr(plhs[0]),mxGetPi(plhs[0]),m*m);
free(a);
free(piv);
magma_free(dwork);
magma_free(da);
magma_queue_destroy(queue);
magma_finalize();
}
I compliled it with mex CC=gcc LDFLAGS="-lmagma -lcudart -lcublas" magmaZinv.c then from matlab, I ran:
a=magic(3)+magic(3)*1i;
magmaZinv(a)
Standalone C code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stddef.h>
#include <magma_v2.h>
#include <cuda_runtime.h>
#include <sys/time.h>
#include <time.h>
/*gateway function*/
int main() {
/*initialize magma*/
magma_init();
magma_queue_t queue = NULL;
magma_device_t dev;
magma_getdevice(&dev);
magma_queue_create(dev,&queue );
int m,ldwork,info;
int *piv;
magmaDoubleComplex *a,*da,*dwork;
/* allocate and initialize a = magic(3)+magic(3)*1i; */
m=3;
piv=(int*) malloc(m*sizeof(int));
ldwork = m*magma_get_zgetri_nb(m);
magma_zmalloc_cpu(&a,m*m);
a[0].x=8;a[0].y=8;
a[1].x=3;a[1].y=3;
a[2].x=4;a[2].y=4;
a[3].x=1;a[3].y=1;
a[4].x=5;a[4].y=5;
a[5].x=9;a[5].y=9;
a[6].x=6;a[6].y=6;
a[7].x=7;a[7].y=7;
a[8].x=2;a[8].y=2;
/* Host -> GPU */
magma_zmalloc(&dwork,ldwork);
magma_zmalloc(&da,m*m);
magma_zsetmatrix(m,m,a,m,da,m,queue);
/*LU and Inverse */
magma_zgetrf_gpu(m,m,da,m,piv,&info);
magma_zgetri_gpu(m,da,m,piv,dwork,ldwork,&info);
/*GPU -> Host */
magma_zgetmatrix(m,m,da,m,a,m,queue);
/* display inv(a) */
for (int i=0;i<(m*m);i++){
printf("%f +%fi\n",a[i].x,a[i].y);
}
/* free memory */
free(a);
free(piv);
magma_free(dwork);
magma_free(da);
magma_queue_destroy(queue);
magma_finalize();
return 0;
}
I compiled with: gcc -lmagma -lcudart Ccode.c -o Ccode.o
My sys admin has figured out why the standalone C code works while the mexed C code does not. I'll post the reason here incase it is helpful to anyone facing the same issues when using Magma from within Matlab.
The version of Matlab I was using was 2014a. The supported compiler for this version is 4.7.x. I was using a higher version of gcc to compile the code. I've never had a problem with using different versions of GCC with matlab, despite the warning it gives, but for the above code it does matter.
Compile with the MKL_ilp64 flag when using Magma with Matlab to ensure that magma_int_t is int64.
With these two suggestions, Magma can be mexed into matlab with no problems.
Hello i wrote my c program which will be run on linux.
I am trying to make my own shell for linux.
I have the following code below...
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
int main(void){
int i = 0;
int k = 0;
int argsCount = 0;
char inputBuffer[MAX_LINE]; /*buffer to hold command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /*command line arguments */
pid_t tpid ;
pid_t child_pid;
int child_status;
char path[PATH_MAX+1];
char *progpath = strdup(args[0]);
char *prog = basename(progpath);
char temp[MAX_LINE];
}
It'is compiling well but when i try to run the code it gives me segmentation fault error
How can i fix it and why i take this error?
Your main has a wrong signature. You want
int main(int argsCount, char**args) {
and of course you should remove the internal declaration of argCount & args inside your main.
Perhaps you want instead your args & argCount to contain the parsed arguments of your own shell (but you still have to give a good signature to your main, conventionally and very often int main(int argc, char**argv).... you probably want your shell to accept the -c argument as most shells do, this would ease debugging with simplistic test cases). Then you should initialize them, and you should read some line (probably with getline) in a loop.
As I commented, you should compile with all warnings & debug info:
gcc -Wall -Wextra -g yoursource.c -o yourprog
Then use gdb ./yourprog to debug your program (see GDB documentation). valgrind should also be helpful. Of course, be sure to develop on a Linux system!
BTW, your program is not a convincing start for a shell. Use strace on some existing shell to understand what a shell needs to do. Study the source code of some existing free software shell (e.g. sash, fish, GNU bash ...). Read Advanced Linux Programming