The fastest C implementation makes use of SIMD intrinsics to parallelize the core computation. There is nothing preventing us from accessing the same operations from our Cython code as well. Doing so requires that we declare the platform-specific SIMD-enabled functions to Cython and integrate them into the spectral_norm.pyx code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import with_statement | |
| """ | |
| set PROXY_HOST=your_bastion_host | |
| set SERVICE_USER=your_func_user | |
| set LINUX_USER=your_SOID | |
| set LINUX_PWD=your_pwd | |
| """ | |
| import click | |
| import paramiko | |
| import time, sys, os |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/expect -f | |
| set timeout 300 | |
| set usr [lindex $argv 0]; | |
| set pwd [lindex $argv 1]; | |
| set query_file [lindex $argv 2]; | |
| spawn -noecho pbrun $usr & | |
| expect -re "Password:" | |
| send "$pwd\r" | |
| sleep 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %%cython | |
| cdef extern from "stdlib.h": | |
| void *malloc(size_t size) | |
| void free(void *ptr) | |
| void qsort(void *array, size_t count, size_t size, int (*compare)(const void *, const void *)) | |
| cdef int int_compare(const void *a, const void *b): | |
| cdef int ia, ib |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from libc.stdlib cimport malloc | |
| def dynamic(size_t N, size_t M): | |
| cdef long *arr = <long*>malloc(N * M * sizeof(long)) | |
| #Using typed memoryview cast | |
| def dynamic(size_t N, size_t M): | |
| cdef long *arr = <long*>malloc(N * M * sizeof(long)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cdef class _finalizer: | |
| cdef void *_data | |
| def __dealloc__(self): | |
| print "_finalizer.__dealloc__" | |
| if self._data is not NULL: | |
| free(self._data) | |
| cdef void set_base(cnp.ndarray arr, void *carr): | |
| cdef _finalizer f = _finalizer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| cimport numpy as np | |
| cimport cython | |
| from libc.stdio cimport FILE, fopen, fwrite, fscanf, fclose, fprintf, fseek, ftell, SEEK_END, rewind, fread | |
| from numpy cimport float64_t | |
| from libc.stdlib cimport malloc, free | |
| cdef extern from "math.h": | |
| double exp(double) nogil | |
| double log(double) nogil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from libc.stdio cimport * | |
| cdef extern from "stdio.h": | |
| #FILE * fopen ( const char * filename, const char * mode ) | |
| FILE *fopen(const char *, const char *) | |
| #int fclose ( FILE * stream ) | |
| int fclose(FILE *) | |
| #ssize_t getline(char **lineptr, size_t *n, FILE *stream); | |
| ssize_t getline(char **, size_t *, FILE *) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %load_ext cython | |
| %%cython | |
| from libc.stdio cimport FILE, stdout, fprintf | |
| fprintf(stdout, "%s\n", <char *>"Hello, world!") | |
| %%cython | |
| from libc.stdio cimport FILE, stdout, fprintf | |
| fprintf(stdout, "%d\n", <int>123) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # distutils: extra_compile_args = -openmp | |
| # distutils: extra_link_args = -openmp | |
| from cython cimport boundscheck, wraparound | |
| from cython.parallel cimport prange, threadid | |
| from libc.stdio cimport stdout, fprintf | |
| import numpy as np | |
| cdef inline double norm2(double complex z) nogil: |