repo_name
stringlengths 7
79
| path
stringlengths 8
206
| copies
stringclasses 36
values | size
stringlengths 2
6
| content
stringlengths 55
523k
| license
stringclasses 15
values | hash
stringlengths 32
32
| line_mean
float64 7.18
99.5
| line_max
int64 16
979
| alpha_frac
float64 0.3
0.87
| ratio
float64 2.01
8.07
| autogenerated
bool 1
class | config_or_test
bool 2
classes | has_no_keywords
bool 2
classes | has_few_assignments
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SimonVT/cathode | cathode-sync/src/main/java/net/simonvt/cathode/actions/movies/SyncPendingMovies.kt | 1 | 3310 | /*
* Copyright (C) 2017 Simon Vig Therkildsen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.simonvt.cathode.actions.movies
import android.content.Context
import net.simonvt.cathode.actions.ActionManager
import net.simonvt.cathode.actions.ErrorHandlerAction
import net.simonvt.cathode.api.service.MoviesService
import net.simonvt.cathode.common.database.forEach
import net.simonvt.cathode.common.database.getLong
import net.simonvt.cathode.common.event.ItemsUpdatedEvent
import net.simonvt.cathode.provider.DatabaseContract.ListItemColumns
import net.simonvt.cathode.provider.DatabaseContract.MovieColumns
import net.simonvt.cathode.provider.ProviderSchematic.ListItems
import net.simonvt.cathode.provider.ProviderSchematic.Movies
import net.simonvt.cathode.provider.entity.ItemTypeString
import net.simonvt.cathode.provider.helper.MovieDatabaseHelper
import net.simonvt.cathode.provider.query
import timber.log.Timber
import javax.inject.Inject
class SyncPendingMovies @Inject constructor(
private val context: Context,
private val syncMovie: SyncMovie,
val moviesService: MoviesService,
val movieHelper: MovieDatabaseHelper
) : ErrorHandlerAction<Unit>() {
override fun key(params: Unit): String = "SyncPendingMovies"
override suspend fun invoke(params: Unit) {
val syncItems = mutableMapOf<Long, Long>()
val where = (MovieColumns.NEEDS_SYNC + "=1 AND (" +
MovieColumns.WATCHED + "=1 OR " +
MovieColumns.IN_COLLECTION + "=1 OR " +
MovieColumns.IN_WATCHLIST + "=1 OR " +
MovieColumns.HIDDEN_CALENDAR + "=1)")
val userMovies = context.contentResolver.query(
Movies.MOVIES,
arrayOf(MovieColumns.ID, MovieColumns.TRAKT_ID),
where
)
userMovies.forEach { cursor ->
val movieId = cursor.getLong(MovieColumns.ID)
val traktId = cursor.getLong(MovieColumns.TRAKT_ID)
if (syncItems[movieId] == null) {
syncItems[movieId] = traktId
}
}
userMovies.close()
val listMovies = context.contentResolver.query(
ListItems.LIST_ITEMS,
arrayOf(ListItemColumns.ITEM_ID),
ListItemColumns.ITEM_TYPE + "=?",
arrayOf(ItemTypeString.MOVIE)
)
listMovies.forEach { cursor ->
val movieId = cursor.getLong(ListItemColumns.ITEM_ID)
if (syncItems[movieId] == null) {
val needsSync = movieHelper.needsSync(movieId)
if (needsSync) {
val traktId = movieHelper.getTraktId(movieId)
syncItems[movieId] = traktId
}
}
}
listMovies.close()
syncItems.forEach { (_, traktId) ->
Timber.d("Syncing pending movie %d", traktId)
ActionManager.invokeSync(syncMovie, SyncMovie.Params(traktId))
if (stopped) {
return
}
}
ItemsUpdatedEvent.post()
}
}
| apache-2.0 | 4bd56ea08dafe56d59d1d28833bb2cc9 | 33.123711 | 75 | 0.721148 | 4.142678 | false | false | false | false |
cempo/SimpleTodoList | app/src/main/java/com/makeevapps/simpletodolist/ui/activity/SnoozeActivity.kt | 1 | 3072 | package com.makeevapps.simpletodolist.ui.activity
import android.app.Activity
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.makeevapps.simpletodolist.Keys.KEY_POSITION
import com.makeevapps.simpletodolist.Keys.KEY_TASK_ID
import com.makeevapps.simpletodolist.R
import com.makeevapps.simpletodolist.databinding.ActivitySnoozeBinding
import com.makeevapps.simpletodolist.datasource.db.table.Task
import com.makeevapps.simpletodolist.viewmodel.SnoozeViewModel
class SnoozeActivity : AppCompatActivity() {
companion object {
val SNOOZE_DATE_REQUEST_CODE = 444
fun getActivityIntent(context: Context, taskId: String, newTask: Boolean, position: Int? = null): Intent {
val intent = Intent(context, SnoozeActivity::class.java)
if (newTask) {
intent.flags = FLAG_ACTIVITY_NEW_TASK
}
intent.putExtra(KEY_TASK_ID, taskId)
intent.putExtra(KEY_POSITION, position)
return intent
}
}
val model: SnoozeViewModel by lazy {
ViewModelProviders.of(this).get(SnoozeViewModel::class.java)
}
val binding: ActivitySnoozeBinding by lazy {
DataBindingUtil.setContentView<ActivitySnoozeBinding>(this, R.layout.activity_snooze)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
title = ""
model.initTask(intent.extras)
binding.controller = this
binding.model = model
if (savedInstanceState == null) {
observeTaskResponse()
}
}
private fun observeTaskResponse() {
model.getTaskResponse().observe(this, Observer<Task> { task ->
if (task != null) {
binding.task = task
val dueDate = model.task.dueDate
val isAllDay = model.task.allDay
startActivityForResult(DateTimeChooserActivity.getActivityIntent(this, dueDate, isAllDay),
DateTimeChooserActivity.CHOOSE_DATE_REQUEST_CODE)
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == DateTimeChooserActivity.CHOOSE_DATE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
data?.let {
model.updateDueDate(it.extras)
model.insertOrUpdateTask(onSuccess = {
setResult(Activity.RESULT_OK, model.resultIntent())
finish()
})
}
} else {
setResult(Activity.RESULT_CANCELED, model.resultIntent())
finish()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
} | mit | 30e75861827c144446412e686c644d22 | 33.920455 | 114 | 0.647135 | 4.853081 | false | false | false | false |
renyuneyun/Easer | app/src/main/java/ryey/easer/core/ui/version_n_info/Version.kt | 1 | 3855 | /*
* Copyright (c) 2016 - 2019 Rui Zhao <[email protected]>
*
* This file is part of Easer.
*
* Easer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easer. If not, see <http://www.gnu.org/licenses/>.
*/
package ryey.easer.core.ui.version_n_info
import android.app.AlertDialog
import android.content.Context
import android.content.SharedPreferences
import android.text.method.LinkMovementMethod
import android.view.View
import android.widget.TextView
import androidx.annotation.StringRes
import ryey.easer.R
import ryey.easer.core.data.storage.StorageHelper
object Version {
private fun showPrompt(context: Context, @StringRes title: Int, @StringRes body: Int, @StringRes vararg fmt: Int) {
val db = AlertDialog.Builder(context)
.setTitle(title)
.setPositiveButton(R.string.button_understand) { dialogInterface, i -> dialogInterface.dismiss() }
if (fmt.isEmpty()) {
db.setMessage(body)
} else {
db.setMessage(context.getString(body).format(*fmt.map { res -> context.getString(res) }.toTypedArray()))
}
(db.show().findViewById<View>(android.R.id.message) as TextView).movementMethod = LinkMovementMethod.getInstance()
}
private fun showVersionPrompt(context: Context, positiveAction: (context: Context) -> Unit, @StringRes body: Int, @StringRes vararg fmt: Int) {
val db = AlertDialog.Builder(context)
.setTitle(R.string.message_future_change_title)
.setPositiveButton(R.string.button_understand) { dialogInterface, _ ->
run {
positiveAction(context)
dialogInterface.dismiss()
}
}
.setNegativeButton(R.string.button_read_next_time) { dialogInterface, _ ->
dialogInterface.dismiss()
}
if (fmt.isEmpty()) {
db.setMessage(body)
} else {
db.setMessage(context.getString(body).format(*fmt.map { res -> context.getString(res) }.toTypedArray()))
}
(db.show().findViewById<View>(android.R.id.message) as TextView).movementMethod = LinkMovementMethod.getInstance()
}
fun dataVersionChange(context: Context) {
if (StorageHelper.hasOldData(context)) {
showPrompt(context, R.string.alert_update_storage_data_title, R.string.alert_update_storage_data)
}
}
fun nearFutureChange(context: Context) {
if (shouldShowVersionPrompt(context, PREF_DEPRECATE_TREE_VIEW)) { //v0.8
showVersionPrompt(context, {c -> setVersionPromptKnown(c, PREF_DEPRECATE_TREE_VIEW)}, R.string.message_future_change_0_8)
}
}
private fun shouldShowVersionPrompt(context: Context, name: String): Boolean {
val prefVersion: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
return !prefVersion.getBoolean(name, false)
}
private fun setVersionPromptKnown(context: Context, name: String) {
val prefVersion: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefVersion.edit().putBoolean(name, true).apply()
}
const val PREFS_NAME = "version_change"
const val PREF_DEPRECATE_TREE_VIEW = "deprecate_tree_view"
} | gpl-3.0 | 9093bd0e9d6a49928c49961de782c7bf | 41.844444 | 147 | 0.672633 | 4.226974 | false | false | false | false |
FHannes/intellij-community | platform/projectModel-impl/src/com/intellij/openapi/module/EmptyModuleManager.kt | 3 | 1856 | /*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.module
import com.intellij.openapi.project.Project
import com.intellij.util.messages.MessageBus
import org.jetbrains.annotations.NotNull
class EmptyModuleManager(project: Project, messageBus: MessageBus) : ModuleManager() {
override fun hasModuleGroups() = false
override fun newModule(filePath: String, moduleTypeId: String) = throw UnsupportedOperationException()
override fun loadModule(filePath: String) = throw UnsupportedOperationException()
override fun disposeModule(module: Module) {
}
override fun getModules() = emptyArray<Module>()
override fun findModuleByName(name: String) = null
override fun getSortedModules() = emptyArray<Module>()
override fun moduleDependencyComparator() = throw UnsupportedOperationException()
override fun getModuleDependentModules(module: Module) = emptyList<Module>()
override fun isModuleDependent(@NotNull module: Module, @NotNull onModule: Module) = false
override fun moduleGraph() = moduleGraph(true)
override fun moduleGraph(includeTests: Boolean) = throw UnsupportedOperationException()
override fun getModifiableModel() = throw UnsupportedOperationException()
override fun getModuleGroupPath(module: Module) = emptyArray<String>()
} | apache-2.0 | 7fb3b437ee42e445b974cd0f8a50a14a | 35.411765 | 104 | 0.776401 | 4.80829 | false | false | false | false |
danrien/projectBlueWater | projectBlueWater/src/test/java/com/lasthopesoftware/bluewater/client/playback/engine/GivenAPlayingPlaybackEngine/WhenPlaybackIsPausedAndPositionIsChangedAndRestarted.kt | 1 | 4296 | package com.lasthopesoftware.bluewater.client.playback.engine.GivenAPlayingPlaybackEngine
import com.lasthopesoftware.bluewater.client.browsing.items.media.files.ServiceFile
import com.lasthopesoftware.bluewater.client.browsing.library.access.PassThroughLibraryStorage
import com.lasthopesoftware.bluewater.client.browsing.library.access.PassThroughSpecificLibraryProvider
import com.lasthopesoftware.bluewater.client.browsing.library.repository.Library
import com.lasthopesoftware.bluewater.client.playback.engine.PlaybackEngine
import com.lasthopesoftware.bluewater.client.playback.engine.bootstrap.PlaylistPlaybackBootstrapper
import com.lasthopesoftware.bluewater.client.playback.engine.preparation.PreparedPlaybackQueueResourceManagement
import com.lasthopesoftware.bluewater.client.playback.file.PositionedFile
import com.lasthopesoftware.bluewater.client.playback.file.PositionedPlayingFile
import com.lasthopesoftware.bluewater.client.playback.file.preparation.FakeDeferredPlayableFilePreparationSourceProvider
import com.lasthopesoftware.bluewater.client.playback.file.preparation.queues.CompletingFileQueueProvider
import com.lasthopesoftware.bluewater.client.playback.view.nowplaying.storage.NowPlaying
import com.lasthopesoftware.bluewater.client.playback.view.nowplaying.storage.NowPlayingRepository
import com.lasthopesoftware.bluewater.client.playback.volume.PlaylistVolumeManager
import com.lasthopesoftware.bluewater.shared.promises.extensions.toFuture
import org.assertj.core.api.Assertions.assertThat
import org.joda.time.Duration
import org.junit.BeforeClass
import org.junit.Test
import java.util.*
class WhenPlaybackIsPausedAndPositionIsChangedAndRestarted {
companion object {
private var playbackEngine: PlaybackEngine? = null
private var nowPlaying: NowPlaying? = null
private val positionedFiles: MutableList<PositionedPlayingFile> = ArrayList()
@BeforeClass
@JvmStatic
fun before() {
val fakePlaybackPreparerProvider = FakeDeferredPlayableFilePreparationSourceProvider()
val library = Library()
library.setId(1)
val libraryProvider = PassThroughSpecificLibraryProvider(library)
val libraryStorage = PassThroughLibraryStorage()
val nowPlayingRepository = NowPlayingRepository(libraryProvider, libraryStorage)
playbackEngine =
PlaybackEngine(
PreparedPlaybackQueueResourceManagement(
fakePlaybackPreparerProvider
) { 1 }, listOf(CompletingFileQueueProvider()),
nowPlayingRepository,
PlaylistPlaybackBootstrapper(PlaylistVolumeManager(1.0f))
)
playbackEngine
?.setOnPlayingFileChanged { f -> positionedFiles.add(f) }
?.startPlaylist(
listOf(
ServiceFile(1),
ServiceFile(2),
ServiceFile(3),
ServiceFile(4),
ServiceFile(5)
), 0, Duration.ZERO
)
val playingPlaybackHandler = fakePlaybackPreparerProvider.deferredResolution.resolve()
fakePlaybackPreparerProvider.deferredResolution.resolve()
playingPlaybackHandler.resolve()
playbackEngine?.pause()
nowPlaying =
playbackEngine
?.skipToNext()
?.eventually { playbackEngine!!.skipToNext() }
?.then { playbackEngine!!.resume() }
?.then { fakePlaybackPreparerProvider.deferredResolution.resolve() }
?.eventually { nowPlayingRepository.nowPlaying }
?.toFuture()
?.get()
}
}
@Test
fun thenThePlaybackStateIsPlaying() {
assertThat(
playbackEngine!!.isPlaying
).isTrue
}
@Test
fun thenTheSavedPlaylistPositionIsCorrect() {
assertThat(
nowPlaying!!.playlistPosition
).isEqualTo(3)
}
@Test
fun thenTheSavedPlaylistIsCorrect() {
assertThat(
nowPlaying!!.playlist
)
.containsExactly(
ServiceFile(1),
ServiceFile(2),
ServiceFile(3),
ServiceFile(4),
ServiceFile(5)
)
}
@Test
fun thenTheObservedFileIsCorrect() {
assertThat(
positionedFiles[positionedFiles.size - 1].playlistPosition
).isEqualTo(3)
}
@Test
fun thenTheFirstSkippedFileIsOnlyObservedOnce() {
assertThat(
positionedFiles
.map { obj -> obj.asPositionedFile() })
.containsOnlyOnce(PositionedFile(1, ServiceFile(2)))
}
@Test
fun thenTheSecondSkippedFileIsNotObserved() {
assertThat(
positionedFiles
.map { obj -> obj.asPositionedFile() })
.doesNotContain(PositionedFile(2, ServiceFile(3)))
}
}
| lgpl-3.0 | 3035ff3c76d6754d27845cb0b3427b4c | 33.368 | 120 | 0.790037 | 4.415211 | false | true | false | false |
TeamAmaze/AmazeFileManager | app/src/main/java/com/amaze/filemanager/asynchronous/services/ftp/FtpService.kt | 1 | 19577 | /*
* Copyright (C) 2014-2021 Arpit Khurana <[email protected]>, Vishal Nehra <[email protected]>,
* Emmanuel Messulam<[email protected]>, Raymond Lai <airwave209gt at gmail.com> and Contributors.
*
* This file is part of Amaze File Manager.
*
* Amaze File Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.amaze.filemanager.asynchronous.services.ftp
import android.app.AlarmManager
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_ONE_SHOT
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.net.wifi.WifiManager
import android.os.Build.VERSION.SDK_INT
import android.os.Build.VERSION_CODES.KITKAT
import android.os.Build.VERSION_CODES.LOLLIPOP
import android.os.Build.VERSION_CODES.M
import android.os.Build.VERSION_CODES.N
import android.os.Build.VERSION_CODES.Q
import android.os.Environment
import android.os.IBinder
import android.os.PowerManager
import android.os.SystemClock
import android.provider.DocumentsContract
import androidx.preference.PreferenceManager
import com.amaze.filemanager.BuildConfig
import com.amaze.filemanager.R
import com.amaze.filemanager.application.AppConfig
import com.amaze.filemanager.asynchronous.services.AbstractProgressiveService.getPendingIntentFlag
import com.amaze.filemanager.filesystem.ftpserver.AndroidFileSystemFactory
import com.amaze.filemanager.filesystem.ftpserver.RootFileSystemFactory
import com.amaze.filemanager.ui.fragments.preferencefragments.PreferencesConstants.PREFERENCE_ROOTMODE
import com.amaze.filemanager.ui.notifications.FtpNotification
import com.amaze.filemanager.ui.notifications.NotificationConstants
import com.amaze.filemanager.utils.ObtainableServiceBinder
import com.amaze.filemanager.utils.PasswordUtil
import org.apache.ftpserver.ConnectionConfigFactory
import org.apache.ftpserver.FtpServer
import org.apache.ftpserver.FtpServerFactory
import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory
import org.apache.ftpserver.listener.ListenerFactory
import org.apache.ftpserver.ssl.ClientAuth
import org.apache.ftpserver.ssl.impl.DefaultSslConfiguration
import org.apache.ftpserver.usermanager.impl.BaseUser
import org.apache.ftpserver.usermanager.impl.WritePermission
import org.greenrobot.eventbus.EventBus
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.IOException
import java.net.InetAddress
import java.net.NetworkInterface
import java.net.UnknownHostException
import java.security.GeneralSecurityException
import java.security.KeyStore
import java.util.*
import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.TrustManagerFactory
import kotlin.concurrent.thread
/**
* Created by yashwanthreddyg on 09-06-2016.
*
*
* Edited by zent-co on 30-07-2019 Edited by bowiechen on 2019-10-19.
*/
class FtpService : Service(), Runnable {
private val binder: IBinder = ObtainableServiceBinder(this)
// Service will broadcast via event bus when server start/stop
enum class FtpReceiverActions {
STARTED, STARTED_FROM_TILE, STOPPED, FAILED_TO_START
}
private var username: String? = null
private var password: String? = null
private var isPasswordProtected = false
private var isStartedByTile = false
private lateinit var wakeLock: PowerManager.WakeLock
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
isStartedByTile = true == intent?.getBooleanExtra(TAG_STARTED_BY_TILE, false)
var attempts = 10
while (serverThread != null) {
if (attempts > 0) {
attempts--
try {
Thread.sleep(1000)
} catch (ignored: InterruptedException) {
}
} else {
return START_STICKY
}
}
serverThread = thread(block = this::run)
val notification = FtpNotification.startNotification(applicationContext, isStartedByTile)
startForeground(NotificationConstants.FTP_ID, notification)
return START_NOT_STICKY
}
override fun onCreate() {
super.onCreate()
val powerManager = getSystemService(POWER_SERVICE) as PowerManager
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, javaClass.name)
wakeLock.setReferenceCounted(false)
}
override fun onBind(intent: Intent): IBinder {
return binder
}
@Suppress("LongMethod")
override fun run() {
val preferences = PreferenceManager.getDefaultSharedPreferences(this)
FtpServerFactory().run {
val connectionConfigFactory = ConnectionConfigFactory()
val shouldUseAndroidFileSystem =
preferences.getBoolean(KEY_PREFERENCE_SAF_FILESYSTEM, false)
if (SDK_INT >= KITKAT && shouldUseAndroidFileSystem) {
fileSystem = AndroidFileSystemFactory(applicationContext)
} else if (preferences.getBoolean(PREFERENCE_ROOTMODE, false)) {
fileSystem = RootFileSystemFactory()
} else {
fileSystem = NativeFileSystemFactory()
}
commandFactory = CommandFactoryFactory.create(shouldUseAndroidFileSystem)
val usernamePreference = preferences.getString(
KEY_PREFERENCE_USERNAME,
DEFAULT_USERNAME
)
if (usernamePreference != DEFAULT_USERNAME) {
username = usernamePreference
runCatching {
password = PasswordUtil.decryptPassword(
applicationContext,
preferences.getString(KEY_PREFERENCE_PASSWORD, "")!!
)
isPasswordProtected = true
}.onFailure {
log.warn("failed to decrypt password in ftp service", it)
AppConfig.toast(applicationContext, R.string.error)
preferences.edit().putString(KEY_PREFERENCE_PASSWORD, "").apply()
isPasswordProtected = false
}
}
val user = BaseUser()
if (!isPasswordProtected) {
user.name = "anonymous"
connectionConfigFactory.isAnonymousLoginEnabled = true
} else {
user.name = username
user.password = password
}
user.homeDirectory = preferences.getString(
KEY_PREFERENCE_PATH,
defaultPath(this@FtpService)
)
if (!preferences.getBoolean(KEY_PREFERENCE_READONLY, false)) {
user.authorities = listOf(WritePermission())
}
connectionConfig = connectionConfigFactory.createConnectionConfig()
userManager.save(user)
val fac = ListenerFactory()
if (preferences.getBoolean(KEY_PREFERENCE_SECURE, DEFAULT_SECURE)) {
try {
val keyStore = KeyStore.getInstance("BKS")
val keyStorePassword = BuildConfig.FTP_SERVER_KEYSTORE_PASSWORD.toCharArray()
keyStore.load(resources.openRawResource(R.raw.key), keyStorePassword)
val keyManagerFactory = KeyManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm())
keyManagerFactory.init(keyStore, keyStorePassword)
val trustManagerFactory = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm())
trustManagerFactory.init(keyStore)
fac.sslConfiguration = DefaultSslConfiguration(
keyManagerFactory,
trustManagerFactory,
ClientAuth.WANT,
"TLS",
enabledCipherSuites,
"ftpserver"
)
fac.isImplicitSsl = true
} catch (e: GeneralSecurityException) {
preferences.edit().putBoolean(KEY_PREFERENCE_SECURE, false).apply()
} catch (e: IOException) {
preferences.edit().putBoolean(KEY_PREFERENCE_SECURE, false).apply()
}
}
fac.port = getPort(preferences)
fac.idleTimeout = preferences.getInt(KEY_PREFERENCE_TIMEOUT, DEFAULT_TIMEOUT)
addListener("default", fac.createListener())
runCatching {
server = createServer().apply {
start()
EventBus.getDefault()
.post(
if (isStartedByTile) {
FtpReceiverActions.STARTED_FROM_TILE
} else {
FtpReceiverActions.STARTED
}
)
}
}.onFailure {
EventBus.getDefault().post(FtpReceiverActions.FAILED_TO_START)
}
}
}
override fun onDestroy() {
wakeLock.release()
serverThread?.let { serverThread ->
serverThread.interrupt()
// wait 10 sec for server thread to finish
serverThread.join(10000)
if (!serverThread.isAlive) {
Companion.serverThread = null
}
server?.stop().also {
EventBus.getDefault().post(FtpReceiverActions.STOPPED)
}
}
}
// Restart the service if the app is closed from the recent list
override fun onTaskRemoved(rootIntent: Intent) {
super.onTaskRemoved(rootIntent)
val restartService = Intent(applicationContext, this.javaClass).setPackage(packageName)
val flag = getPendingIntentFlag(FLAG_ONE_SHOT)
val restartServicePI = PendingIntent.getService(
applicationContext,
1,
restartService,
flag
)
val alarmService = applicationContext.getSystemService(ALARM_SERVICE) as AlarmManager
alarmService[AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 2000] =
restartServicePI
}
companion object {
private val log: Logger = LoggerFactory.getLogger(FtpService::class.java)
const val DEFAULT_PORT = 2211
const val DEFAULT_USERNAME = ""
const val DEFAULT_TIMEOUT = 600 // default timeout, in sec
const val DEFAULT_SECURE = true
const val PORT_PREFERENCE_KEY = "ftpPort"
const val KEY_PREFERENCE_PATH = "ftp_path"
const val KEY_PREFERENCE_USERNAME = "ftp_username"
const val KEY_PREFERENCE_PASSWORD = "ftp_password_encrypted"
const val KEY_PREFERENCE_TIMEOUT = "ftp_timeout"
const val KEY_PREFERENCE_SECURE = "ftp_secure"
const val KEY_PREFERENCE_READONLY = "ftp_readonly"
const val KEY_PREFERENCE_SAF_FILESYSTEM = "ftp_saf_filesystem"
const val KEY_PREFERENCE_ROOT_FILESYSTEM = "ftp_root_filesystem"
const val INITIALS_HOST_FTP = "ftp://"
const val INITIALS_HOST_SFTP = "ftps://"
// RequestStartStopReceiver listens for these actions to start/stop this server
const val ACTION_START_FTPSERVER =
"com.amaze.filemanager.services.ftpservice.FTPReceiver.ACTION_START_FTPSERVER"
const val ACTION_STOP_FTPSERVER =
"com.amaze.filemanager.services.ftpservice.FTPReceiver.ACTION_STOP_FTPSERVER"
const val TAG_STARTED_BY_TILE = "started_by_tile"
// attribute of action_started, used by notification
private lateinit var _enabledCipherSuites: Array<String>
init {
_enabledCipherSuites = LinkedList<String>().apply {
if (SDK_INT >= Q) {
add("TLS_AES_128_GCM_SHA256")
add("TLS_AES_256_GCM_SHA384")
add("TLS_CHACHA20_POLY1305_SHA256")
}
if (SDK_INT >= N) {
add("TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256")
add("TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256")
}
if (SDK_INT >= LOLLIPOP) {
add("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA")
add("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256")
add("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA")
add("TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384")
add("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA")
add("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")
add("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA")
add("TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384")
add("TLS_RSA_WITH_AES_128_GCM_SHA256")
add("TLS_RSA_WITH_AES_256_GCM_SHA384")
}
if (SDK_INT < LOLLIPOP) {
add("TLS_RSA_WITH_AES_128_CBC_SHA")
add("TLS_RSA_WITH_AES_256_CBC_SHA")
}
}.toTypedArray()
}
/**
* Return a list of available ciphers for ftpserver.
*
* Added SDK detection since some ciphers are available only on higher versions, and they
* have to be on top of the list to make a more secure SSL
*
* @see [org.apache.ftpserver.ssl.SslConfiguration]
* @see [javax.net.ssl.SSLEngine]
*/
@JvmStatic
val enabledCipherSuites = _enabledCipherSuites
private var serverThread: Thread? = null
private var server: FtpServer? = null
/**
* Derive the FTP server's default share path, depending the user's Android version.
*
* Default it's the internal storage's root as java.io.File; otherwise it's content://
* based URI if it's running on Android 7.0 or above.
*/
@JvmStatic
fun defaultPath(context: Context): String {
return if (PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean(KEY_PREFERENCE_SAF_FILESYSTEM, false) && SDK_INT > M
) {
DocumentsContract.buildTreeDocumentUri(
"com.android.externalstorage.documents",
"primary:"
).toString()
} else {
Environment.getExternalStorageDirectory().absolutePath
}
}
/**
* Indicator whether FTP service is running
*/
@JvmStatic
fun isRunning(): Boolean {
val server = server ?: return false
return !server.isStopped
}
/**
* Is the device connected to local network, either Ethernet or Wifi?
*/
@JvmStatic
fun isConnectedToLocalNetwork(context: Context): Boolean {
val cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
var connected: Boolean
if (SDK_INT >= M) {
connected = cm.activeNetwork?.let { activeNetwork ->
cm.getNetworkCapabilities(activeNetwork)?.let { ni ->
ni.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) or
ni.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
} ?: false
} ?: false
} else {
connected = cm.activeNetworkInfo?.let { ni ->
ni.isConnected && (
ni.type and (
ConnectivityManager.TYPE_WIFI
or ConnectivityManager.TYPE_ETHERNET
) != 0
)
} ?: false
}
if (!connected) {
connected = runCatching {
NetworkInterface.getNetworkInterfaces().toList().find { netInterface ->
netInterface.displayName.startsWith("rndis") or
netInterface.displayName.startsWith("wlan")
}
}.getOrElse { null } != null
}
return connected
}
/**
* Is the device connected to Wifi?
*/
@JvmStatic
fun isConnectedToWifi(context: Context): Boolean {
val cm = context.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
return if (SDK_INT >= M) {
cm.activeNetwork?.let {
cm.getNetworkCapabilities(it)?.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
} ?: false
} else {
cm.activeNetworkInfo?.let {
it.isConnected && it.type == ConnectivityManager.TYPE_WIFI
} ?: false
}
}
/**
* Determine device's IP address
*/
@JvmStatic
fun getLocalInetAddress(context: Context): InetAddress? {
if (!isConnectedToLocalNetwork(context)) {
return null
}
if (isConnectedToWifi(context)) {
val wm = context.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val ipAddress = wm.connectionInfo.ipAddress
return if (ipAddress == 0) null else intToInet(ipAddress)
}
runCatching {
NetworkInterface.getNetworkInterfaces().iterator().forEach { netinterface ->
netinterface.inetAddresses.iterator().forEach { address ->
// this is the condition that sometimes gives problems
if (!address.isLoopbackAddress &&
!address.isLinkLocalAddress
) {
return address
}
}
}
}.onFailure { e ->
log.warn("failed to get local inet address", e)
}
return null
}
private fun intToInet(value: Int): InetAddress? {
val bytes = ByteArray(4)
for (i in 0..3) {
bytes[i] = byteOfInt(value, i)
}
return try {
InetAddress.getByAddress(bytes)
} catch (e: UnknownHostException) {
// This only happens if the byte array has a bad length
null
}
}
private fun byteOfInt(value: Int, which: Int): Byte {
val shift = which * 8
return (value shr shift).toByte()
}
private fun getPort(preferences: SharedPreferences): Int {
return preferences.getInt(PORT_PREFERENCE_KEY, DEFAULT_PORT)
}
}
}
| gpl-3.0 | 454c21d8f43f9c36b9a5c05a851cafc5 | 40.041929 | 107 | 0.596516 | 5.061272 | false | false | false | false |
QuickBlox/quickblox-android-sdk | sample-conference-kotlin/app/src/main/java/com/quickblox/sample/conference/kotlin/presentation/screens/chatinfo/ChatInfoViewModel.kt | 1 | 4002 | package com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo
import android.os.Bundle
import com.quickblox.chat.model.QBChatDialog
import com.quickblox.sample.conference.kotlin.domain.DomainCallback
import com.quickblox.sample.conference.kotlin.domain.chat.ChatManager
import com.quickblox.sample.conference.kotlin.domain.repositories.db.DBRepository
import com.quickblox.sample.conference.kotlin.domain.user.UserManager
import com.quickblox.sample.conference.kotlin.presentation.LiveData
import com.quickblox.sample.conference.kotlin.presentation.screens.appinfo.ViewState
import com.quickblox.sample.conference.kotlin.presentation.screens.base.BaseViewModel
import com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo.ViewState.Companion.DIALOG_LOADED
import com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo.ViewState.Companion.ERROR
import com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo.ViewState.Companion.PROGRESS
import com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo.ViewState.Companion.SHOW_SEARCH_USER_SCREEN
import com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo.ViewState.Companion.USERS_LOADED
import com.quickblox.sample.conference.kotlin.presentation.screens.chatinfo.ViewState.Companion.USERS_UPDATED
import com.quickblox.users.model.QBUser
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
/*
* Created by Injoit in 2021-09-30.
* Copyright © 2021 Quickblox. All rights reserved.
*/
@HiltViewModel
class ChatInfoViewModel @Inject constructor(private val userManager: UserManager, private val chatManager: ChatManager, private val dbRepository: DBRepository) : BaseViewModel() {
val liveData = LiveData<Pair<Int, Any?>>()
val users = arrayListOf<QBUser>()
var currentDialog: QBChatDialog? = null
private set
var currentUser: QBUser? = null
private set
init {
currentUser = userManager.getCurrentUser()
}
override fun onStartView() {
if (!chatManager.isLoggedInChat()) {
loginToChat()
}
}
override fun onStopApp() {
chatManager.destroyChat()
}
private fun loginToChat() {
currentUser?.let {
chatManager.loginToChat(it, object : DomainCallback<Unit, Exception> {
override fun onSuccess(result: Unit, bundle: Bundle?) {
// empty
}
override fun onError(error: Exception) {
liveData.setValue(Pair(ERROR, error.message))
}
})
} ?: run {
liveData.setValue(Pair(ViewState.SHOW_LOGIN_SCREEN, null))
}
}
fun getCurrentUserId(): Int? {
return dbRepository.getCurrentUser()?.id
}
fun loadUsersByIds() {
liveData.setValue(Pair(PROGRESS, null))
currentDialog?.occupants?.let {
userManager.loadUsersByIds(it, object : DomainCallback<ArrayList<QBUser>, Exception> {
override fun onSuccess(result: ArrayList<QBUser>, bundle: Bundle?) {
users.addAll(result)
liveData.setValue(Pair(USERS_LOADED, null))
}
override fun onError(error: Exception) {
liveData.setValue(Pair(ERROR, error.message))
}
})
}
}
fun loadDialogById(dialogId: String) {
users.clear()
liveData.setValue(Pair(PROGRESS, null))
for (dialog in chatManager.getDialogs()) {
if (dialog.dialogId == dialogId) {
currentDialog = dialog
liveData.setValue(Pair(DIALOG_LOADED, null))
break
}
}
}
override fun onResumeView() {
super.onResumeView()
liveData.setValue(Pair(USERS_UPDATED, null))
}
fun addUser() {
liveData.setValue(Pair(SHOW_SEARCH_USER_SCREEN, null))
}
} | bsd-3-clause | bacfefcb7316d7b1960d15bd4aa85b95 | 36.401869 | 179 | 0.68108 | 4.674065 | false | false | false | false |
tasks/tasks | app/src/main/java/org/tasks/etebase/EtebaseLocalCache.kt | 1 | 4028 | package org.tasks.etebase
import android.content.Context
import com.etebase.client.*
import com.etebase.client.Collection
import com.etebase.client.exceptions.EtebaseException
import com.etebase.client.exceptions.UrlParseException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.*
class EtebaseLocalCache private constructor(context: Context, username: String) {
private val fsCache: FileSystemCache = FileSystemCache.create(context.filesDir.absolutePath, username)
private suspend fun clearUserCache() {
withContext(Dispatchers.IO) {
fsCache.clearUserCache()
}
}
suspend fun saveStoken(stoken: String) {
withContext(Dispatchers.IO) {
fsCache.saveStoken(stoken)
}
}
suspend fun loadStoken(): String? = withContext(Dispatchers.IO) {
fsCache.loadStoken()
}
suspend fun collectionList(colMgr: CollectionManager): List<Collection> =
withContext(Dispatchers.IO) {
fsCache._unstable_collectionList(colMgr).filter { !it.isDeleted }
}
suspend fun collectionGet(colMgr: CollectionManager, colUid: String): Collection =
withContext(Dispatchers.IO) {
fsCache.collectionGet(colMgr, colUid)
}
suspend fun collectionSet(colMgr: CollectionManager, collection: Collection) {
if (collection.isDeleted) {
collectionUnset(colMgr, collection.uid)
} else {
withContext(Dispatchers.IO) {
fsCache.collectionSet(colMgr, collection)
}
}
}
suspend fun collectionUnset(colMgr: CollectionManager, collection: RemovedCollection) {
collectionUnset(colMgr, collection.uid())
}
private suspend fun collectionUnset(colMgr: CollectionManager, colUid: String) {
withContext(Dispatchers.IO) {
try {
fsCache.collectionUnset(colMgr, colUid)
} catch (e: UrlParseException) {
// Ignore, as it just means the file doesn't exist
}
}
}
suspend fun itemGet(itemMgr: ItemManager, colUid: String, itemUid: String): Item? =
withContext(Dispatchers.IO) {
// Need the try because the inner call doesn't return null on missing, but an error
try {
fsCache.itemGet(itemMgr, colUid, itemUid)
} catch (e: EtebaseException) {
null
}
}
suspend fun itemSet(itemMgr: ItemManager, colUid: String, item: Item) {
withContext(Dispatchers.IO) {
if (item.isDeleted) {
try {
fsCache.itemUnset(itemMgr, colUid, item.uid)
} catch (e: UrlParseException) {
// Ignore, as it just means the file doesn't exist
}
} else {
fsCache.itemSet(itemMgr, colUid, item)
}
}
}
companion object {
private val localCacheCache: HashMap<String, EtebaseLocalCache> = HashMap()
fun getInstance(context: Context, username: String): EtebaseLocalCache {
synchronized(localCacheCache) {
val cached = localCacheCache[username]
return if (cached != null) {
cached
} else {
val ret = EtebaseLocalCache(context, username)
localCacheCache[username] = ret
ret
}
}
}
fun clear(context: Context) = runBlocking {
val users = synchronized(localCacheCache) {
localCacheCache.keys.toList()
}
users.forEach { clear(context, it) }
}
suspend fun clear(context: Context, username: String) {
val localCache = getInstance(context, username)
localCache.clearUserCache()
localCacheCache.remove(username)
}
}
} | gpl-3.0 | acaae1d85dee9cb7a0a0ab390df4a23b | 32.857143 | 106 | 0.601787 | 4.683721 | false | false | false | false |
ademar111190/Studies | Projects/Reddit/app/src/test/java/ademar/study/reddit/presenter/home/HomePresenterTest.kt | 1 | 8812 | package ademar.study.reddit.presenter.home
import ademar.study.reddit.core.interactor.GetPostsUseCase
import ademar.study.reddit.core.model.Error
import ademar.study.reddit.core.model.Post
import ademar.study.reddit.mapper.ErrorMapper
import ademar.study.reddit.mapper.home.PostMapper
import ademar.study.reddit.model.ErrorViewModel
import ademar.study.reddit.model.home.PostViewModel
import ademar.study.reddit.navigation.FlowController
import ademar.study.reddit.test.BaseTest
import ademar.study.reddit.test.Fixture
import io.reactivex.Observable
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.`when` as whenever
class HomePresenterTest : BaseTest() {
@Mock lateinit var mockFlowController: FlowController
@Mock lateinit var mockGetPostsUseCase: GetPostsUseCase
@Mock lateinit var mockPostMapper: PostMapper
@Mock lateinit var mockErrorMapper: ErrorMapper
private lateinit var mockPost: Post
private lateinit var mockError: Error
private lateinit var mockPostViewModel: PostViewModel
private lateinit var mockErrorViewModel: ErrorViewModel
private var bindPostCount = 0
private var showContentCount = 0
private var showErrorCount = 0
private var showLoadingCount = 0
private var showRetryCount = 0
private var hideUnloadedErrorCount = 0
private var showUnloadedPostsCount = 0
private var hideUnloadedPostsCount = 0
private var showUnloadedErrorCount = 0
private var clearPostsCount = 0
@Before
override fun setUp() {
super.setUp()
mockPost = Fixture.post.makeModel()
mockError = Fixture.error.makeModel()
mockPostViewModel = Fixture.postViewModel.makeModel()
mockErrorViewModel = Fixture.errorViewModel.makeModel()
whenever(mockPostMapper.transform(mockPost)).thenReturn(mockPostViewModel)
whenever(mockErrorMapper.transform(mockError)).thenReturn(mockErrorViewModel)
bindPostCount = 0
showContentCount = 0
showErrorCount = 0
showLoadingCount = 0
showRetryCount = 0
hideUnloadedErrorCount = 0
showUnloadedPostsCount = 0
hideUnloadedPostsCount = 0
showUnloadedErrorCount = 0
clearPostsCount = 0
}
@Test
fun testOnAttachView() {
val stubView = object : StubHomeView() {}
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
}
@Test
fun testOnStart_success() {
val stubView = object : StubHomeView() {
override fun showLoading() {
showLoadingCount++
}
override fun bindPost(viewModel: PostViewModel) {
assertThat(viewModel).isEqualTo(mockPostViewModel)
bindPostCount++
}
override fun showContent() {
showContentCount++
}
override fun clearPosts() {
clearPostsCount++
}
}
whenever(mockGetPostsUseCase.currentPage()).thenReturn(Observable.just(mockPost))
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
presenter.onStart()
assertThat(showLoadingCount).isEqualTo(1)
assertThat(bindPostCount).isEqualTo(1)
assertThat(showContentCount).isEqualTo(1)
assertThat(clearPostsCount).isEqualTo(1)
}
@Test
fun testOnStart_error() {
val stubView = object : StubHomeView() {
override fun showLoading() {
showLoadingCount++
}
override fun showError(viewModel: ErrorViewModel) {
assertThat(viewModel).isEqualTo(mockErrorViewModel)
showErrorCount++
}
override fun showRetry() {
showRetryCount++
}
override fun clearPosts() {
clearPostsCount++
}
}
whenever(mockGetPostsUseCase.currentPage()).thenReturn(Observable.error(mockError))
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
presenter.onStart()
assertThat(showLoadingCount).isEqualTo(1)
assertThat(showErrorCount).isEqualTo(1)
assertThat(showRetryCount).isEqualTo(1)
assertThat(clearPostsCount).isEqualTo(1)
}
@Test
fun testOnReloadClick_success() {
val stubView = object : StubHomeView() {
override fun showLoading() {
showLoadingCount++
}
override fun bindPost(viewModel: PostViewModel) {
assertThat(viewModel).isEqualTo(mockPostViewModel)
bindPostCount++
}
override fun showContent() {
showContentCount++
}
override fun clearPosts() {
clearPostsCount++
}
}
whenever(mockGetPostsUseCase.currentPage()).thenReturn(Observable.just(mockPost))
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
presenter.onReloadClick()
assertThat(showLoadingCount).isEqualTo(1)
assertThat(bindPostCount).isEqualTo(1)
assertThat(showContentCount).isEqualTo(1)
assertThat(clearPostsCount).isEqualTo(1)
}
@Test
fun testOnReloadClick_error() {
val stubView = object : StubHomeView() {
override fun showLoading() {
showLoadingCount++
}
override fun showError(viewModel: ErrorViewModel) {
assertThat(viewModel).isEqualTo(mockErrorViewModel)
showErrorCount++
}
override fun showRetry() {
showRetryCount++
}
override fun clearPosts() {
clearPostsCount++
}
}
whenever(mockGetPostsUseCase.currentPage()).thenReturn(Observable.error(mockError))
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
presenter.onReloadClick()
assertThat(showLoadingCount).isEqualTo(1)
assertThat(showErrorCount).isEqualTo(1)
assertThat(showRetryCount).isEqualTo(1)
assertThat(clearPostsCount).isEqualTo(1)
}
@Test
fun testOnPreviousPageClick_success() {
val stubView = object : StubHomeView() {
override fun hideUnloadedError() {
hideUnloadedErrorCount++
}
override fun showUnloadedPosts() {
showUnloadedPostsCount++
}
override fun bindPost(viewModel: PostViewModel) {
assertThat(viewModel).isEqualTo(mockPostViewModel)
bindPostCount++
}
}
whenever(mockGetPostsUseCase.previousPage()).thenReturn(Observable.just(mockPost))
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
presenter.onPreviousPageClick()
assertThat(hideUnloadedErrorCount).isEqualTo(1)
assertThat(showUnloadedPostsCount).isEqualTo(1)
assertThat(bindPostCount).isEqualTo(1)
}
@Test
fun testOnPreviousPageClick_error() {
val stubView = object : StubHomeView() {
override fun hideUnloadedError() {
hideUnloadedErrorCount++
}
override fun showUnloadedPosts() {
showUnloadedPostsCount++
}
override fun showUnloadedError(viewModel: ErrorViewModel) {
assertThat(viewModel).isEqualTo(mockErrorViewModel)
showUnloadedErrorCount++
}
}
whenever(mockGetPostsUseCase.previousPage()).thenReturn(Observable.error(mockError))
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onAttachView(stubView)
presenter.onPreviousPageClick()
assertThat(hideUnloadedErrorCount).isEqualTo(1)
assertThat(showUnloadedPostsCount).isEqualTo(1)
assertThat(showUnloadedErrorCount).isEqualTo(1)
}
@Test
fun testOnDetachView() {
val presenter = HomePresenter(mockFlowController, mockGetPostsUseCase, mockPostMapper, mockErrorMapper)
presenter.onDetachView()
}
}
| mit | ad5a51dbc85be76451ac879310c7c402 | 31.880597 | 111 | 0.655243 | 5.044076 | false | true | false | false |
apollo-rsps/apollo | game/src/main/kotlin/org/apollo/game/action/ActionCoroutine.kt | 1 | 4204 | package org.apollo.game.action
import java.util.concurrent.CancellationException
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.*
typealias ActionPredicate = () -> Boolean
typealias ActionBlock = suspend ActionCoroutine.() -> Unit
interface ActionCoroutineCondition {
/**
* Called once every tick to check if `Continuation` associated with this condition should be resumed.
*/
fun resume(): Boolean
}
/**
* A continuation condition that waits on a given number of pulses.
*/
class AwaitPulses(pulses: Int) : ActionCoroutineCondition {
val remainingPulses: AtomicInteger = AtomicInteger(pulses)
override fun resume(): Boolean {
return remainingPulses.decrementAndGet() <= 0
}
}
/**
* A continuation condition that waits until a predicate is fufilled.
*/
class AwaitPredicate(val predicate: ActionPredicate) : ActionCoroutineCondition {
override fun resume(): Boolean {
return predicate.invoke()
}
}
/**
* A suspend point in an `ActionCoroutine` that has its `continuation` resumed whenever the `condition` evaluates
* to `true`.
*/
data class ActionCoroutineStep(val condition: ActionCoroutineCondition, internal val continuation: Continuation<Unit>)
@RestrictsSuspension
class ActionCoroutine : Continuation<Unit> {
companion object {
/**
* Create a new `ActionCoroutine` and immediately execute the given `block`, returning a continuation that
* can be resumed.
*/
fun start(block: ActionBlock): ActionCoroutine {
val coroutine = ActionCoroutine()
val continuation = block.createCoroutine(coroutine, coroutine)
coroutine.resumeContinuation(continuation)
return coroutine
}
}
override val context: CoroutineContext = EmptyCoroutineContext
override fun resumeWith(result: Result<Unit>) {
if (result.isFailure) {
throw result.exceptionOrNull()!!
}
}
private fun resumeContinuation(continuation: Continuation<Unit>, allowCancellation: Boolean = true) {
try {
continuation.resume(Unit)
} catch (ex: CancellationException) {
if (!allowCancellation) {
throw ex
}
}
}
/**
* The next `step` in this `ActionCoroutine` saved as a resume point.
*/
private var next = AtomicReference<ActionCoroutineStep>()
/**
* Check if this continuation has no more steps to execute.
*/
fun stopped(): Boolean {
return next.get() == null
}
/**
* Update this continuation and check if the condition for the next step to be resumed is satisfied.
*/
fun pulse() {
val nextStep = next.getAndSet(null) ?: return
val condition = nextStep.condition
val continuation = nextStep.continuation
if (condition.resume()) {
resumeContinuation(continuation)
} else {
next.compareAndSet(null, nextStep)
}
}
private suspend fun awaitCondition(condition: ActionCoroutineCondition) {
return suspendCoroutineUninterceptedOrReturn { cont ->
next.compareAndSet(null, ActionCoroutineStep(condition, cont))
COROUTINE_SUSPENDED
}
}
/**
* Stop execution of this continuation.
*/
suspend fun stop(): Nothing {
suspendCancellableCoroutine<Unit> { cont ->
next.set(null)
cont.cancel()
}
error("Tried to resume execution a coroutine that should have been cancelled.")
}
/**
* Wait `pulses` game updates before resuming this continuation.
*/
suspend fun wait(pulses: Int = 1) = awaitCondition(AwaitPulses(pulses))
/**
* Wait until the `predicate` returns `true` before resuming this continuation.
*/
suspend fun wait(predicate: ActionPredicate) = awaitCondition(AwaitPredicate(predicate))
} | isc | f77206d4181eff427d153f7823e2d7bd | 29.693431 | 118 | 0.671028 | 5.040767 | false | false | false | false |
wiltonlazary/kotlin-native | backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/MethodBridge.kt | 1 | 4278 | /*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package org.jetbrains.kotlin.backend.konan.objcexport
import org.jetbrains.kotlin.backend.common.descriptors.allParameters
import org.jetbrains.kotlin.backend.konan.ir.allParameters
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.ParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
internal sealed class TypeBridge
internal object ReferenceBridge : TypeBridge()
internal data class BlockPointerBridge(
val numberOfParameters: Int,
val returnsVoid: Boolean
) : TypeBridge()
internal data class ValueTypeBridge(val objCValueType: ObjCValueType) : TypeBridge()
internal sealed class MethodBridgeParameter
internal sealed class MethodBridgeReceiver : MethodBridgeParameter() {
object Static : MethodBridgeReceiver()
object Factory : MethodBridgeReceiver()
object Instance : MethodBridgeReceiver()
}
internal object MethodBridgeSelector : MethodBridgeParameter()
internal sealed class MethodBridgeValueParameter : MethodBridgeParameter() {
data class Mapped(val bridge: TypeBridge) : MethodBridgeValueParameter()
object ErrorOutParameter : MethodBridgeValueParameter()
object SuspendCompletion : MethodBridgeValueParameter()
}
internal data class MethodBridge(
val returnBridge: ReturnValue,
val receiver: MethodBridgeReceiver,
val valueParameters: List<MethodBridgeValueParameter>
) {
sealed class ReturnValue {
object Void : ReturnValue()
object HashCode : ReturnValue()
data class Mapped(val bridge: TypeBridge) : ReturnValue()
sealed class Instance : ReturnValue() {
object InitResult : Instance()
object FactoryResult : Instance()
}
sealed class WithError : ReturnValue() {
object Success : WithError()
data class ZeroForError(val successBridge: ReturnValue, val successMayBeZero: Boolean) : WithError()
}
object Suspend : ReturnValue()
}
val paramBridges: List<MethodBridgeParameter> =
listOf(receiver) + MethodBridgeSelector + valueParameters
// TODO: it is not exactly true in potential future cases.
val isInstance: Boolean get() = when (receiver) {
MethodBridgeReceiver.Static,
MethodBridgeReceiver.Factory -> false
MethodBridgeReceiver.Instance -> true
}
val returnsError: Boolean
get() = returnBridge is ReturnValue.WithError
}
internal fun MethodBridge.valueParametersAssociated(
descriptor: FunctionDescriptor
): List<Pair<MethodBridgeValueParameter, ParameterDescriptor?>> {
val kotlinParameters = descriptor.allParameters.iterator()
val skipFirstKotlinParameter = when (this.receiver) {
MethodBridgeReceiver.Static -> false
MethodBridgeReceiver.Factory, MethodBridgeReceiver.Instance -> true
}
if (skipFirstKotlinParameter) {
kotlinParameters.next()
}
return this.valueParameters.map {
when (it) {
is MethodBridgeValueParameter.Mapped -> it to kotlinParameters.next()
MethodBridgeValueParameter.SuspendCompletion,
is MethodBridgeValueParameter.ErrorOutParameter -> it to null
}
}.also { assert(!kotlinParameters.hasNext()) }
}
internal fun MethodBridge.parametersAssociated(
irFunction: IrFunction
): List<Pair<MethodBridgeParameter, IrValueParameter?>> {
val kotlinParameters = irFunction.allParameters.iterator()
return this.paramBridges.map {
when (it) {
is MethodBridgeValueParameter.Mapped, MethodBridgeReceiver.Instance ->
it to kotlinParameters.next()
MethodBridgeValueParameter.SuspendCompletion,
MethodBridgeReceiver.Static, MethodBridgeSelector, MethodBridgeValueParameter.ErrorOutParameter ->
it to null
MethodBridgeReceiver.Factory -> {
kotlinParameters.next()
it to null
}
}
}.also { assert(!kotlinParameters.hasNext()) }
}
| apache-2.0 | 9f3cc920fae711a9b3c958783447a98c | 34.355372 | 112 | 0.717625 | 5.52 | false | false | false | false |
wiltonlazary/kotlin-native | backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/InlineClasses.kt | 1 | 12654 | /*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
package org.jetbrains.kotlin.backend.konan
import org.jetbrains.kotlin.backend.konan.ir.containsNull
import org.jetbrains.kotlin.backend.konan.ir.getSuperClassNotAny
import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.FqNameUnsafe
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperClassifiers
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.isNullable
import org.jetbrains.kotlin.types.typeUtil.makeNullable
/**
* TODO: there is [IrType::getInlinedClass] in [org.jetbrains.kotlin.ir.util] which isn't compatible with
* Native's implementation. Please take a look while commonization.
*/
fun IrType.getInlinedClassNative(): IrClass? = IrTypeInlineClassesSupport.getInlinedClass(this)
/**
* TODO: there is [IrType::isInlined] in [org.jetbrains.kotlin.ir.util] which isn't compatible with
* Native's implementation. Please take a look while commonization.
*/
fun IrType.isInlinedNative(): Boolean = IrTypeInlineClassesSupport.isInlined(this)
fun IrClass.isInlined(): Boolean = IrTypeInlineClassesSupport.isInlined(this)
fun KotlinType.getInlinedClass(): ClassDescriptor? = KotlinTypeInlineClassesSupport.getInlinedClass(this)
fun ClassDescriptor.isInlined(): Boolean = KotlinTypeInlineClassesSupport.isInlined(this)
fun KotlinType.binaryRepresentationIsNullable() = KotlinTypeInlineClassesSupport.representationIsNullable(this)
internal inline fun <R> KotlinType.unwrapToPrimitiveOrReference(
eachInlinedClass: (inlinedClass: ClassDescriptor, nullable: Boolean) -> Unit,
ifPrimitive: (primitiveType: KonanPrimitiveType, nullable: Boolean) -> R,
ifReference: (type: KotlinType) -> R
): R = KotlinTypeInlineClassesSupport.unwrapToPrimitiveOrReference(this, eachInlinedClass, ifPrimitive, ifReference)
// TODO: consider renaming to `isReference`.
fun KotlinType.binaryTypeIsReference(): Boolean = this.computePrimitiveBinaryTypeOrNull() == null
fun IrType.binaryTypeIsReference(): Boolean = this.computePrimitiveBinaryTypeOrNull() == null
fun KotlinType.computePrimitiveBinaryTypeOrNull(): PrimitiveBinaryType? =
this.computeBinaryType().primitiveBinaryTypeOrNull()
fun KotlinType.computeBinaryType(): BinaryType<ClassDescriptor> = KotlinTypeInlineClassesSupport.computeBinaryType(this)
fun IrType.computePrimitiveBinaryTypeOrNull(): PrimitiveBinaryType? =
this.computeBinaryType().primitiveBinaryTypeOrNull()
fun IrType.computeBinaryType(): BinaryType<IrClass> = IrTypeInlineClassesSupport.computeBinaryType(this)
fun IrClass.inlinedClassIsNullable(): Boolean = this.defaultType.makeNullable().getInlinedClassNative() == this // TODO: optimize
fun IrClass.isUsedAsBoxClass(): Boolean = IrTypeInlineClassesSupport.isUsedAsBoxClass(this)
/**
* Most "underlying" user-visible non-reference type.
* It is visible as inlined to compiler for simplicity.
*/
enum class KonanPrimitiveType(val classId: ClassId, val binaryType: BinaryType.Primitive) {
BOOLEAN(PrimitiveType.BOOLEAN, PrimitiveBinaryType.BOOLEAN),
CHAR(PrimitiveType.CHAR, PrimitiveBinaryType.SHORT),
BYTE(PrimitiveType.BYTE, PrimitiveBinaryType.BYTE),
SHORT(PrimitiveType.SHORT, PrimitiveBinaryType.SHORT),
INT(PrimitiveType.INT, PrimitiveBinaryType.INT),
LONG(PrimitiveType.LONG, PrimitiveBinaryType.LONG),
FLOAT(PrimitiveType.FLOAT, PrimitiveBinaryType.FLOAT),
DOUBLE(PrimitiveType.DOUBLE, PrimitiveBinaryType.DOUBLE),
NON_NULL_NATIVE_PTR(ClassId.topLevel(KonanFqNames.nonNullNativePtr.toSafe()), PrimitiveBinaryType.POINTER),
VECTOR128(ClassId.topLevel(KonanFqNames.Vector128), PrimitiveBinaryType.VECTOR128)
;
constructor(primitiveType: PrimitiveType, primitiveBinaryType: PrimitiveBinaryType)
: this(ClassId.topLevel(primitiveType.typeFqName), primitiveBinaryType)
constructor(classId: ClassId, primitiveBinaryType: PrimitiveBinaryType)
: this(classId, BinaryType.Primitive(primitiveBinaryType))
val fqName: FqNameUnsafe get() = this.classId.asSingleFqName().toUnsafe()
companion object {
val byFqName = KonanPrimitiveType.values().associateBy { it.fqName }
}
}
internal abstract class InlineClassesSupport<Class : Any, Type : Any> {
protected abstract fun isNullable(type: Type): Boolean
protected abstract fun makeNullable(type: Type): Type
protected abstract fun erase(type: Type): Class
protected abstract fun computeFullErasure(type: Type): Sequence<Class>
protected abstract fun getFqName(clazz: Class): FqNameUnsafe
protected abstract fun hasInlineModifier(clazz: Class): Boolean
protected abstract fun getNativePointedSuperclass(clazz: Class): Class?
protected abstract fun getInlinedClassUnderlyingType(clazz: Class): Type
@JvmName("classIsInlined")
fun isInlined(clazz: Class): Boolean = getInlinedClass(clazz) != null
fun isInlined(type: Type): Boolean = getInlinedClass(type) != null
fun isUsedAsBoxClass(clazz: Class) = getInlinedClass(clazz) == clazz // To handle NativePointed subclasses.
fun getInlinedClass(type: Type): Class? =
getInlinedClass(erase(type), isNullable(type))
private fun getInlinedClass(erased: Class, isNullable: Boolean): Class? {
val inlinedClass = getInlinedClass(erased) ?: return null
return if (!isNullable || representationIsNonNullReferenceOrPointer(inlinedClass)) {
inlinedClass
} else {
null
}
}
tailrec fun representationIsNonNullReferenceOrPointer(clazz: Class): Boolean {
val konanPrimitiveType = KonanPrimitiveType.byFqName[getFqName(clazz)]
if (konanPrimitiveType != null) {
return konanPrimitiveType == KonanPrimitiveType.NON_NULL_NATIVE_PTR
}
val inlinedClass = getInlinedClass(clazz) ?: return true
val underlyingType = getInlinedClassUnderlyingType(inlinedClass)
return if (isNullable(underlyingType)) {
false
} else {
representationIsNonNullReferenceOrPointer(erase(underlyingType))
}
}
@JvmName("classGetInlinedClass")
private fun getInlinedClass(clazz: Class): Class? =
if (hasInlineModifier(clazz) || getFqName(clazz) in implicitInlineClasses) {
clazz
} else {
getNativePointedSuperclass(clazz)
}
inline fun <R> unwrapToPrimitiveOrReference(
type: Type,
eachInlinedClass: (inlinedClass: Class, nullable: Boolean) -> Unit,
ifPrimitive: (primitiveType: KonanPrimitiveType, nullable: Boolean) -> R,
ifReference: (type: Type) -> R
): R {
var currentType: Type = type
while (true) {
val inlinedClass = getInlinedClass(currentType)
if (inlinedClass == null) {
return ifReference(currentType)
}
val nullable = isNullable(currentType)
KonanPrimitiveType.byFqName[getFqName(inlinedClass)]?.let { primitiveType ->
return ifPrimitive(primitiveType, nullable)
}
eachInlinedClass(inlinedClass, nullable)
val underlyingType = getInlinedClassUnderlyingType(inlinedClass)
currentType = if (nullable) makeNullable(underlyingType) else underlyingType
}
}
fun representationIsNullable(type: Type): Boolean {
unwrapToPrimitiveOrReference(
type,
eachInlinedClass = { _, nullable -> if (nullable) return true },
ifPrimitive = { _, nullable -> return nullable },
ifReference = { return isNullable(it) }
)
}
// TODO: optimize.
fun computeBinaryType(type: Type): BinaryType<Class> {
val erased = erase(type)
val inlinedClass = getInlinedClass(erased, isNullable(type)) ?: return createReferenceBinaryType(type)
KonanPrimitiveType.byFqName[getFqName(inlinedClass)]?.let {
return it.binaryType
}
val underlyingBinaryType = computeBinaryType(getInlinedClassUnderlyingType(inlinedClass))
return if (isNullable(type) && underlyingBinaryType is BinaryType.Reference) {
BinaryType.Reference(underlyingBinaryType.types, true)
} else {
underlyingBinaryType
}
}
private fun createReferenceBinaryType(type: Type): BinaryType.Reference<Class> =
BinaryType.Reference(computeFullErasure(type), true)
}
private val implicitInlineClasses =
(KonanPrimitiveType.values().map { it.fqName } +
KonanFqNames.nativePtr +
InteropFqNames.cPointer).toSet()
internal object KotlinTypeInlineClassesSupport : InlineClassesSupport<ClassDescriptor, KotlinType>() {
override fun isNullable(type: KotlinType): Boolean = type.isNullable()
override fun makeNullable(type: KotlinType): KotlinType = type.makeNullable()
override tailrec fun erase(type: KotlinType): ClassDescriptor {
val descriptor = type.constructor.declarationDescriptor
return if (descriptor is ClassDescriptor) {
descriptor
} else {
erase(type.constructor.supertypes.first())
}
}
override fun computeFullErasure(type: KotlinType): Sequence<ClassDescriptor> {
val classifier = type.constructor.declarationDescriptor
return if (classifier is ClassDescriptor) sequenceOf(classifier)
else type.constructor.supertypes.asSequence().flatMap { computeFullErasure(it) }
}
override fun getFqName(clazz: ClassDescriptor): FqNameUnsafe = clazz.fqNameUnsafe
override fun hasInlineModifier(clazz: ClassDescriptor): Boolean = clazz.isInline
override fun getNativePointedSuperclass(clazz: ClassDescriptor): ClassDescriptor? = clazz.getAllSuperClassifiers()
.firstOrNull { it.fqNameUnsafe == InteropFqNames.nativePointed } as ClassDescriptor?
override fun getInlinedClassUnderlyingType(clazz: ClassDescriptor): KotlinType =
clazz.unsubstitutedPrimaryConstructor!!.valueParameters.single().type
}
private object IrTypeInlineClassesSupport : InlineClassesSupport<IrClass, IrType>() {
override fun isNullable(type: IrType): Boolean = type.containsNull()
override fun makeNullable(type: IrType): IrType = type.makeNullable()
override tailrec fun erase(type: IrType): IrClass {
val classifier = type.classifierOrFail
return when (classifier) {
is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> erase(classifier.owner.superTypes.first())
else -> error(classifier)
}
}
override fun computeFullErasure(type: IrType): Sequence<IrClass> {
val classifier = type.classifierOrFail
return when (classifier) {
is IrClassSymbol -> sequenceOf(classifier.owner)
is IrTypeParameterSymbol -> classifier.owner.superTypes.asSequence().flatMap { computeFullErasure(it) }
else -> error(classifier)
}
}
override fun getFqName(clazz: IrClass): FqNameUnsafe = clazz.descriptor.fqNameUnsafe
override fun hasInlineModifier(clazz: IrClass): Boolean = clazz.descriptor.isInline
override fun getNativePointedSuperclass(clazz: IrClass): IrClass? {
var superClass: IrClass? = clazz
while (superClass != null && superClass.descriptor.fqNameUnsafe != InteropFqNames.nativePointed)
superClass = superClass.getSuperClassNotAny()
return superClass
}
override fun getInlinedClassUnderlyingType(clazz: IrClass): IrType =
clazz.constructors.firstOrNull { it.isPrimary }?.valueParameters?.single()?.type
?: clazz.declarations.filterIsInstance<IrProperty>().single { it.backingField != null }.backingField!!.type
}
| apache-2.0 | 9bd5112f1be7a60f8fb33e6662e39efa | 43.713781 | 129 | 0.729967 | 4.865052 | false | false | false | false |
DankBots/Mega-Gnar | src/main/kotlin/gg/octave/bot/listeners/VoiceListener.kt | 1 | 3652 | package gg.octave.bot.listeners
import gg.octave.bot.Launcher
import gg.octave.bot.db.OptionsRegistry
import net.dv8tion.jda.api.events.GenericEvent
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent
import net.dv8tion.jda.api.hooks.EventListener
class VoiceListener : EventListener {
override fun onEvent(event: GenericEvent) {
when (event) {
is GuildVoiceJoinEvent -> onGuildVoiceJoin(event)
is GuildVoiceLeaveEvent -> onGuildVoiceLeave(event)
is GuildVoiceMoveEvent -> onGuildVoiceMove(event)
}
}
private fun onGuildVoiceJoin(event: GuildVoiceJoinEvent) {
if (!Launcher.loaded) {
return
}
if (event.member.user == event.jda.selfUser) {
return
}
val guild = event.guild
Launcher.players.getExisting(guild.idLong)?.let {
if (!it.guild?.selfMember!!.voiceState!!.inVoiceChannel()) {
Launcher.players.destroy(guild.idLong)
} else if (it.isAlone()) {
it.queueLeave()
} else {
it.cancelLeave()
}
}
}
private fun onGuildVoiceLeave(event: GuildVoiceLeaveEvent) {
if (!Launcher.loaded) {
return
}
if (event.member.user == event.jda.selfUser) {
return Launcher.players.destroy(event.guild.idLong)
}
val guild = event.guild
Launcher.players.getExisting(guild.idLong)?.let {
if (!it.guild?.selfMember!!.voiceState!!.inVoiceChannel()) {
Launcher.players.destroy(guild.idLong)
} else if (it.isAlone()) {
it.queueLeave()
}
}
}
private fun onGuildVoiceMove(event: GuildVoiceMoveEvent) {
if (!Launcher.loaded) {
return
}
if (event.member.user == event.jda.selfUser) {
if (!event.guild.selfMember.voiceState!!.inVoiceChannel()) {
return Launcher.players.destroy(event.guild.idLong)
}
if (event.channelJoined.id == event.guild.afkChannel?.id) {
return Launcher.players.destroy(event.guild.idLong)
}
Launcher.players.getExisting(event.guild.idLong)?.let {
val options = OptionsRegistry.ofGuild(event.guild)
if (options.music.channels.isNotEmpty()) {
if (event.channelJoined.id !in options.music.channels) {
it.currentRequestChannel?.let { requestChannel ->
requestChannel.sendMessage("Cannot join `${event.channelJoined.name}`, it isn't one of the designated music channels.")
.queue()
}
return Launcher.players.destroy(event.guild.idLong)
}
}
// it.moveAudioConnection(event.channelJoined)
if (it.isAlone()) {
it.queueLeave()
} else {
it.cancelLeave()
}
}
return
}
val guild = event.guild
Launcher.players.getExisting(guild.idLong)?.let {
when {
!event.guild.selfMember.voiceState!!.inVoiceChannel() -> Launcher.players.destroy(event.guild.idLong)
it.isAlone() -> it.queueLeave()
else -> it.cancelLeave()
}
}
}
}
| mit | e4aeadaf4313ecbf34534f2409da5314 | 32.2 | 147 | 0.55805 | 4.593711 | false | false | false | false |
nextcloud/android | app/src/main/java/com/nextcloud/client/jobs/CalendarBackupWork.kt | 1 | 3003 | /*
*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2021 Tobias Kaminsky
* Copyright (C) 2021 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.client.jobs
import android.content.ContentResolver
import android.content.Context
import android.text.TextUtils
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.nextcloud.client.account.UserAccountManager
import com.nextcloud.client.preferences.AppPreferences
import com.owncloud.android.lib.common.utils.Log_OC
import third_parties.sufficientlysecure.AndroidCalendar
import third_parties.sufficientlysecure.SaveCalendar
import java.util.Calendar
class CalendarBackupWork(
appContext: Context,
params: WorkerParameters,
private val contentResolver: ContentResolver,
private val accountManager: UserAccountManager,
private val preferences: AppPreferences
) : Worker(appContext, params) {
companion object {
val TAG = CalendarBackupWork::class.java.simpleName
const val ACCOUNT = "account"
const val FORCE = "force"
const val JOB_INTERVAL_MS: Long = 24 * 60 * 60 * 1000
}
override fun doWork(): Result {
val accountName = inputData.getString(ACCOUNT) ?: ""
val optionalUser = accountManager.getUser(accountName)
if (!optionalUser.isPresent || TextUtils.isEmpty(accountName)) { // no account provided
Log_OC.d(TAG, "User not present")
return Result.failure()
}
val lastExecution = preferences.calendarLastBackup
val force = inputData.getBoolean(FORCE, false)
if (force || lastExecution + JOB_INTERVAL_MS < Calendar.getInstance().timeInMillis) {
val calendars = AndroidCalendar.loadAll(contentResolver)
Log_OC.d(TAG, "Saving ${calendars.size} calendars")
calendars.forEach { calendar ->
SaveCalendar(
applicationContext,
calendar,
preferences,
optionalUser.get()
).start()
}
// store execution date
preferences.calendarLastBackup = Calendar.getInstance().timeInMillis
} else {
Log_OC.d(TAG, "last execution less than 24h ago")
}
return Result.success()
}
}
| gpl-2.0 | 4fa77eb5544fc90e5cb2e0272c4e85e4 | 35.621951 | 95 | 0.687313 | 4.706897 | false | false | false | false |
soywiz/korge | @old/korge-spriter/src/commonMain/kotlin/com/soywiz/korge/ext/spriter/com/brashmonkey/spriter/SCMLReader.kt | 1 | 11579 | package com.soywiz.korge.ext.spriter.com.brashmonkey.spriter
import com.soywiz.korge.ext.spriter.com.brashmonkey.spriter.Entity.*
import com.soywiz.korge.ext.spriter.com.brashmonkey.spriter.Mainline.Key.*
import com.soywiz.korio.lang.*
import com.soywiz.korio.serialization.xml.*
import com.soywiz.korio.stream.*
/**
* This class parses a SCML file and creates a [Data] instance.
* If you want to keep track of what is going on during the build process of the objects parsed from the SCML file,
* you could extend this class and override the load*() methods for pre or post processing.
* This could be e.g. useful for a loading screen which responds to the current building or parsing state.
* @author Trixt0r
*/
class SCMLReader {
/**
* Returns the loaded SCML data.
* @return the SCML data.
*/
var data: Data
/**
* Creates a new SCML reader and will parse all objects in the given stream.
* @param stream the stream
*/
constructor(stream: SyncStream) {
this.data = this.load(stream)
}
/**
* Creates a new SCML reader and will parse the given xml string.
* @param xml the xml string
*/
constructor(xml: String) {
this.data = this.load(xml)
}
/**
* Parses the SCML object save in the given xml string and returns the build data object.
* @param xml the xml string
* *
* @return the built data
*/
protected fun load(xml: String): Data {
return load(Xml(xml))
}
/**
* Parses the SCML objects saved in the given stream and returns the built data object.
* @param stream the stream from the SCML file
* *
* @return the built data
*/
protected fun load(stream: SyncStream): Data {
return load(Xml(stream.readAll().toString(UTF8)))
}
/**
* Reads the data from the given root element, i.e. the spriter_data node.
* @param root
* *
* @return
*/
protected fun load(root: Xml): Data {
val folders = root.children("folder").toList()
val entities = root.children("entity").toList()
val atlases = root.children("atlas")
.flatMap { it.children("i") }
.map { it.getString("name") ?: "" }
data = Data(
root.getString("scml_version") ?: "",
root.getString("generator") ?: "",
root.getString("generator_version") ?: "",
Data.PixelMode[root.getInt("pixel_mode") ?: 0],
folders.size,
entities.size,
atlases
)
loadFolders(folders)
loadEntities(entities)
return data
}
/**
* Iterates through the given folders and adds them to the current [Data] object.
* @param folders a list of folders to load
*/
protected fun loadFolders(folders: List<Xml>) {
for (i in folders.indices) {
val repo = folders[i]
val files = repo.children("file").toList()
val folder = Folder(repo.getInt("id") ?: 0, repo.getString("name") ?: ("no_name_$i"), files.size)
loadFiles(files, folder)
data.addFolder(folder)
}
}
/**
* Iterates through the given files and adds them to the given [Folder] object.
* @param files a list of files to load
* *
* @param folder the folder containing the files
*/
protected fun loadFiles(files: List<Xml>, folder: Folder) {
for (j in files.indices) {
val f = files[j]
val file = File(
f.getInt("id") ?: 0, f.getString("name") ?: "",
Dimension((f.getInt("width") ?: 0).toFloat(), (f.getInt("height") ?: 0).toFloat()),
Point((f.getDouble("pivot_x") ?: 0.0).toFloat(), (f.getDouble("pivot_y") ?: 1.0).toFloat())
)
folder.addFile(file)
}
}
/**
* Iterates through the given entities and adds them to the current [Data] object.
* @param entities a list of entities to load
*/
protected fun loadEntities(entities: List<Xml>) {
for (i in entities.indices) {
val e = entities[i]
val infos = e.children("obj_info").toList()
val charMaps = e.children("character_map").toList()
val animations = e.children("animation").toList()
val entity =
Entity(e.getInt("id") ?: 0, e.getString("name") ?: "", animations.size, charMaps.size, infos.size)
data.addEntity(entity)
loadObjectInfos(infos, entity)
loadCharacterMaps(charMaps, entity)
loadAnimations(animations, entity)
}
}
/**
* Iterates through the given object infos and adds them to the given [Entity] object.
* @param infos a list of infos to load
* *
* @param entity the entity containing the infos
*/
protected fun loadObjectInfos(infos: List<Xml>, entity: Entity) {
for (i in infos.indices) {
val info = infos[i]
val objInfo = ObjectInfo(
info.get("name", "info" + i),
ObjectType.Companion.getObjectInfoFor(info.get("type", "")),
Dimension(info.getFloat("w", 0f), info.getFloat("h", 0f))
)
entity.addInfo(objInfo)
val frames = info.getChildByName("frames") ?: continue
val frameIndices = frames.getChildrenByName("i")
for (i1 in frameIndices.indices) {
val index = frameIndices[i1]
val folder = index.getInt("folder", 0)
val file = index.getInt("file", 0)
objInfo.frames.add(FileReference(folder, file))
}
}
}
/**
* Iterates through the given character maps and adds them to the given [Entity] object.
* @param maps a list of character maps to load
* *
* @param entity the entity containing the character maps
*/
protected fun loadCharacterMaps(maps: List<Xml>, entity: Entity) {
for (i in maps.indices) {
val map = maps[i]
val charMap = CharacterMap(map.getInt("id", 0), map.getAttribute("name", "charMap" + i))
entity.addCharacterMap(charMap)
val mappings = map.getChildrenByName("map")
for (i1 in mappings.indices) {
val mapping = mappings[i1]
val folder = mapping.getInt("folder", 0)
val file = mapping.getInt("file", 0)
charMap.put(
FileReference(folder, file),
FileReference(mapping.getInt("target_folder", folder), mapping.getInt("target_file", file))
)
}
}
}
/**
* Iterates through the given animations and adds them to the given [Entity] object.
* @param animations a list of animations to load
* *
* @param entity the entity containing the animations maps
*/
protected fun loadAnimations(animations: List<Xml>, entity: Entity) {
for (i in animations.indices) {
val a = animations[i]
val timelines = a.getChildrenByName("timeline")
val mainline = a.getChildByName("mainline")
val mainlineKeys = mainline!!.getChildrenByName("key")!!
val animation = Animation(
Mainline(mainlineKeys.size),
a.getInt("id", 0), a.get("name", ""), a.getInt("length", 0),
a.getBoolean("looping", true), timelines.size
)
entity.addAnimation(animation)
loadMainlineKeys(mainlineKeys, animation.mainline)
loadTimelines(timelines, animation, entity)
animation.prepare()
}
}
/**
* Iterates through the given mainline keys and adds them to the given [Mainline] object.
* @param keys a list of mainline keys
* *
* @param main the mainline
*/
protected fun loadMainlineKeys(keys: List<Xml>, main: Mainline) {
for (i in main.keys.indices) {
val k = keys[i]
val objectRefs = k.getChildrenByName("object_ref")
val boneRefs = k.getChildrenByName("bone_ref")
val curve = Curve()
curve.type = Curve.getType(k.get("curve_type", "linear"))
curve.constraints[k.getFloat("c1", 0f), k.getFloat("c2", 0f), k.getFloat("c3", 0f)] = k.getFloat("c4", 0f)
val key = Mainline.Key(
k.getInt("id", 0), k.getInt("time", 0), curve,
boneRefs.size, objectRefs.size
)
main.addKey(key)
loadRefs(objectRefs, boneRefs, key)
}
}
/**
* Iterates through the given bone and object references and adds them to the given [Mainline.Key] object.
* @param objectRefs a list of object references
* *
* @param boneRefs a list if bone references
* *
* @param key the mainline key
*/
protected fun loadRefs(objectRefs: List<Xml>, boneRefs: List<Xml>, key: Mainline.Key) {
for (i in boneRefs.indices) {
val e = boneRefs[i]
val boneRef = BoneRef(
e.getInt("id", 0), e.getInt("timeline", 0),
e.getInt("key", 0), key.getBoneRef(e.getInt("parent", -1))
)
key.addBoneRef(boneRef)
}
for (i in objectRefs.indices) {
val o = objectRefs[i]
val objectRef = ObjectRef(
o.getInt("id", 0), o.getInt("timeline", 0),
o.getInt("key", 0), key.getBoneRef(o.getInt("parent", -1)), o.getInt("z_index", 0)
)
key.addObjectRef(objectRef)
}
key.objectRefs.sort()
}
/**
* Iterates through the given timelines and adds them to the given [Animation] object.
* @param timelines a list of timelines
* *
* @param animation the animation containing the timelines
* *
* @param entity entity for assigning the timeline an object info
*/
protected fun loadTimelines(timelines: List<Xml>, animation: Animation, entity: Entity) {
for (i in timelines.indices) {
val t = timelines[i]
val keys = timelines[i].getChildrenByName("key")
val name = t.get("name", "")
val type = ObjectType.getObjectInfoFor(t.get("object_type", "sprite"))
var info: ObjectInfo? = entity.getInfo(name)
if (info == null) info = ObjectInfo(name, type, Dimension(0f, 0f))
val timeline = Timeline(t.getInt("id", 0), name, info, keys.size)
animation.addTimeline(timeline)
loadTimelineKeys(keys, timeline)
}
}
/**
* Iterates through the given timeline keys and adds them to the given [Timeline] object.
* @param keys a list if timeline keys
* *
* @param timeline the timeline containing the keys
*/
protected fun loadTimelineKeys(keys: List<Xml>, timeline: Timeline) {
for (i in keys.indices) {
val k = keys[i]
val curve = Curve()
curve.type = Curve.getType(k.get("curve_type", "linear"))
curve.constraints[k.getFloat("c1", 0f), k.getFloat("c2", 0f), k.getFloat("c3", 0f)] = k.getFloat("c4", 0f)
val key = Timeline.Key(k.getInt("id", 0), k.getInt("time", 0), k.getInt("spin", 1), curve)
var obj: Xml? = k.getChildByName("bone")
if (obj == null) obj = k.getChildByName("object")
val position = Point(obj!!.getFloat("x", 0f), obj.getFloat("y", 0f))
val scale = Point(obj.getFloat("scale_x", 1f), obj.getFloat("scale_y", 1f))
var pivot = Point(
obj.getFloat("pivot_x", 0f),
obj.getFloat("pivot_y", if (timeline.objectInfo.type == ObjectType.Bone) .5f else 1f)
)
val angle = obj.getFloat("angle", 0f)
var alpha = 1f
var folder = -1
var file = -1
if (obj.name == "object") {
if (timeline.objectInfo.type == ObjectType.Sprite) {
alpha = obj.getFloat("a", 1f)
folder = obj.getInt("folder", -1)
file = obj.getInt("file", -1)
val f = data.getFolder(folder).getFile(file)
pivot = Point(obj.getFloat("pivot_x", f.pivot!!.x), obj.getFloat("pivot_y", f.pivot.y))
timeline.objectInfo.size.set(f.size!!)
}
}
val `object`: Timeline.Key.Object
if (obj.name == "bone")
`object` = Timeline.Key.Object(position, scale, pivot, angle, alpha, FileReference(folder, file))
else
`object` = Timeline.Key.Object(position, scale, pivot, angle, alpha, FileReference(folder, file))
key.setObject(`object`)
timeline.addKey(key)
}
}
fun Xml.getChildrenByName(name: String) = this.children(name).toList()
fun Xml.getChildByName(name: String) = this.child(name)
fun Xml.get(name: String, default: String = "") = this.getString(name) ?: default
fun Xml.getAttribute(name: String, default: String = "") = this.getString(name) ?: default
fun Xml.getInt(name: String, default: Int) = this.getInt(name) ?: default
fun Xml.getFloat(name: String, default: Float) = this.getDouble(name)?.toFloat() ?: default
fun Xml.getBoolean(name: String, default: Boolean) = when (this.getString(name)) {
null -> default
"true", "TRUE", "1" -> true
else -> false
}
}
| apache-2.0 | 7b85816fd0be6a176f5c1a485cdbd9a8 | 32.465318 | 115 | 0.666638 | 3.276457 | false | false | false | false |
AkshayChordiya/gofun-android | app/src/main/java/com/adityakamble49/ttl/utils/PrefUtils.kt | 1 | 1562 | package com.adityakamble49.ttl.utils
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
/**
* @author Akshay Chordiya
* @since 3/3/2017.
*/
/**
* Get string [SharedPreferences]
* @param context context
* @param key of pref value
* @param defValue of required string
* @return string value
*/
fun getString(context: Context, key: String, defValue: String): String {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
return pref.getString(key, defValue)
}
fun putString(context: Context, key: String, value: String) {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
pref.edit{
putString(key, value)
}
}
fun getLong(context: Context, key: String, defValue: Long): Long {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
return pref.getLong(key, defValue)
}
fun putLong(context: Context, key: String, value: Long) {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
pref.edit{
putLong(key, value)
}
}
fun exists(context: Context, key: String): Boolean {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
return pref.contains(key)
}
fun remove(context: Context, key: String) {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
pref.edit{
remove(key)
}
}
inline fun SharedPreferences.edit(func: SharedPreferences.Editor.() -> Unit) {
val editor = edit()
editor.func()
editor.apply()
} | mit | 8cdf1756e5e8d11bc866dd8e4955fad3 | 25.491525 | 78 | 0.721511 | 4.176471 | false | false | false | false |
stripe/stripe-android | payments-core/src/test/java/com/stripe/android/model/PaymentMethodCreateParamsFixtures.kt | 1 | 3636 | package com.stripe.android.model
internal object PaymentMethodCreateParamsFixtures {
internal val CARD = PaymentMethodCreateParams.Card(
number = "4242424242424242",
expiryMonth = 1,
expiryYear = 2024,
cvc = "111"
)
internal val CARD_WITH_ATTRIBUTION = PaymentMethodCreateParams.Card(
number = "4242424242424242",
expiryMonth = 1,
expiryYear = 2024,
cvc = "111",
attribution = setOf("CardMultilineWidget")
)
@JvmField
internal val BILLING_DETAILS = PaymentMethod.BillingDetails(
name = "Jenny Rosen",
email = "[email protected]",
phone = "1-800-555-1234",
address = Address(
line1 = "1234 Main St",
city = "San Francisco",
state = "CA",
country = "US",
postalCode = "94111"
)
)
@JvmField
internal val DEFAULT_CARD = PaymentMethodCreateParams.create(
CARD,
BILLING_DETAILS
)
internal val DEFAULT_FPX = PaymentMethodCreateParams.create(
PaymentMethodCreateParams.Fpx(
bank = "hsbc"
)
)
internal val DEFAULT_SEPA_DEBIT = PaymentMethodCreateParams.create(
PaymentMethodCreateParams.SepaDebit(iban = "my_iban")
)
internal val AU_BECS_DEBIT = PaymentMethodCreateParams.create(
PaymentMethodCreateParams.AuBecsDebit(
bsbNumber = "000000",
accountNumber = "000123456"
),
BILLING_DETAILS
)
internal val BACS_DEBIT = PaymentMethodCreateParams.create(
bacsDebit = PaymentMethodCreateParams.BacsDebit(
accountNumber = "00012345",
sortCode = "108800"
),
billingDetails = BILLING_DETAILS
)
internal val SOFORT = PaymentMethodCreateParams.create(
sofort = PaymentMethodCreateParams.Sofort(
country = "DE"
),
billingDetails = BILLING_DETAILS
)
internal val P24 = PaymentMethodCreateParams.createP24(
billingDetails = BILLING_DETAILS
)
internal val BANCONTACT = PaymentMethodCreateParams.createBancontact(
billingDetails = BILLING_DETAILS
)
internal val GIROPAY = PaymentMethodCreateParams.createGiropay(
billingDetails = BILLING_DETAILS
)
internal val EPS = PaymentMethodCreateParams.createEps(
billingDetails = BILLING_DETAILS
)
internal val GRABPAY = PaymentMethodCreateParams.createGrabPay(
billingDetails = BILLING_DETAILS
)
internal val UPI = PaymentMethodCreateParams.create(
upi = PaymentMethodCreateParams.Upi(
vpa = "vpa@hdfcbank"
),
billingDetails = BILLING_DETAILS
)
internal val NETBANKING = PaymentMethodCreateParams.create(
netbanking = PaymentMethodCreateParams.Netbanking(
bank = "hdfc"
),
billingDetails = BILLING_DETAILS
)
internal val US_BANK_ACCOUNT = PaymentMethodCreateParams.create(
usBankAccount = PaymentMethodCreateParams.USBankAccount(
accountNumber = "000123456789",
routingNumber = "110000000",
accountType = PaymentMethod.USBankAccount.USBankAccountType.CHECKING,
accountHolderType = PaymentMethod.USBankAccount.USBankAccountHolderType.INDIVIDUAL
),
billingDetails = BILLING_DETAILS
)
@JvmStatic
fun createWith(metadata: Map<String, String>): PaymentMethodCreateParams {
return PaymentMethodCreateParams.create(
CARD,
BILLING_DETAILS,
metadata
)
}
}
| mit | 9b4978453aa6b37a76a320dd5e061b5b | 28.322581 | 94 | 0.636139 | 4.90027 | false | false | false | false |
Devifish/ReadMe | app/src/main/java/cn/devifish/readme/entity/BookDetail.kt | 1 | 1133 | package cn.devifish.readme.entity
import java.util.*
/**
* Created by zhang on 2017/8/4.
*
*/
data class BookDetail(
var _id: String? = null,
var title: String? = null,
var author: String? = null,
var longIntro: String? = null,
var cover: String? = null,
var majorCate: String? = null,
var minorCate: String? = null,
var creater: String? = null,
var sizetype: Int = 0,
var superscript: String? = null,
var currency: Int = 0,
var contentType: String? = null,
var _le: Boolean = false,
var allowMonthly: Boolean = false,
var allowVoucher: Boolean = false,
var allowBeanVoucher: Boolean = false,
var hasCp: Boolean = false,
var postCount: Int = 0,
var latelyFollower: Int = 0,
var followerCount: Int = 0,
var wordCount: Int = 0,
var serializeWordCount: Int = 0,
var retentionRatio: Float = 0.0f,
var updated: Date? = null,
var isSerial: Boolean = false,
var chaptersCount: Int = 0,
var lastChapter: String? = null
) | apache-2.0 | 31f5c2c34ecfe1d3d5933b86edc1cd8d | 29.648649 | 46 | 0.571933 | 3.776667 | false | false | false | false |
ivanTrogrlic/LeagueStats | app/src/main/java/com/ivantrogrlic/leaguestats/main/summoner/SummonerScreenAdapter.kt | 1 | 1215 | package com.ivantrogrlic.leaguestats.main.summoner
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import com.ivantrogrlic.leaguestats.main.home.HomeFragment
import com.ivantrogrlic.leaguestats.main.summoner.games.GamesFragment
import com.ivantrogrlic.leaguestats.main.summoner.ranks.RanksFragment
import com.ivantrogrlic.leaguestats.model.Summoner
import org.parceler.Parcels
/**
* Created by ivan on 8/9/2017.
*/
class SummonerScreenAdapter(manager: FragmentManager, val summoner: Summoner) : FragmentPagerAdapter(manager) {
override fun getItem(position: Int): Fragment {
val bundle = Bundle()
bundle.putParcelable(HomeFragment.SUMMONER_KEY, Parcels.wrap(summoner))
val ranksFragment = RanksFragment()
val gamesFragment = GamesFragment()
ranksFragment.arguments = bundle
gamesFragment.arguments = bundle
return when (position) {
0 -> ranksFragment
1 -> gamesFragment
else -> throw IllegalStateException("Illegal pager position: $position")
}
}
override fun getCount(): Int = 2
}
| apache-2.0 | 60780bd6c2ac1890edec6a2bb60d179f | 33.714286 | 111 | 0.739095 | 3.844937 | false | false | false | false |
android/xAnd11 | core/src/main/java/com/monksanctum/xand11/core/ObjectPool.kt | 1 | 2387 | // Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.monksanctum.xand11.core
internal interface Pool {
fun recycle(recycleable: Any)
}
abstract class ObjectPool<T : ObjectPool.Recycleable, in V> : Pool {
private var mAvailable = mutableListOf<T>()
fun obtain(vararg arg: V): T {
synchronized(mAvailable) {
for (i in mAvailable.indices) {
if (validate(mAvailable[i], *arg)) {
return mAvailable.removeAt(i)
}
}
}
val t = create(*arg)!!
t.mPool = this
return t
}
override fun recycle(recycleable: Any) {
synchronized(mAvailable) {
mAvailable.add(recycleable as T)
}
}
protected open fun validate(inst: T, vararg arg: V): Boolean {
return true
}
protected abstract fun create(vararg arg: V): T?
open class Recycleable {
internal var mPool: Pool? = null
fun close() {
recycle()
}
fun recycle() {
mPool!!.recycle(this)
}
}
}
abstract class IntObjectPool<T : ObjectPool.Recycleable> : Pool {
private var mAvailable = mutableListOf<T>()
fun obtain(vararg arg: Int): T {
synchronized(mAvailable) {
for (i in mAvailable.indices) {
if (validate(mAvailable[i], *arg)) {
return mAvailable.removeAt(i)
}
}
}
val t = create(*arg)!!
t.mPool = this
return t
}
override fun recycle(recycleable: Any) {
synchronized(mAvailable) {
mAvailable.add(recycleable as T)
}
}
protected open fun validate(inst: T, vararg arg: Int): Boolean {
return true
}
protected abstract fun create(vararg arg: Int): T?
}
| apache-2.0 | 2d839778993097df940e76e03ada35b6 | 25.522222 | 75 | 0.591956 | 4.247331 | false | false | false | false |
tbaxter120/Restdroid | app/src/main/java/com/ridocula/restdroid/fragments/NewRequestFragment.kt | 1 | 6270 | package com.ridocula.restdroid.fragments
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.ridocula.restdroid.R
import com.ridocula.restdroid.activities.NewRequestActivity
import com.ridocula.restdroid.models.Header
import com.ridocula.restdroid.models.HistoryRequest
import com.ridocula.restdroid.models.Request
import com.ridocula.restdroid.util.Utility
import java.util.*
/**
* A simple [Fragment] subclass.
* Activities that contain this fragment must implement the
* [NewRequestFragment.OnFragmentInteractionListener] interface
* to handle interaction events.
* Use the [NewRequestFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class NewRequestFragment : Fragment() {
private var mSendRequestListener: OnSendRequestListener? = null
private var etBody : EditText? = null
private var etUrl : EditText? = null
private var spType : Spinner? = null
private var ibAddHeader : ImageButton? = null
private var llHeaders : LinearLayout? = null
private var svRoot : ScrollView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
svRoot = inflater!!.inflate(R.layout.fragment_new_request, container, false) as ScrollView
etBody = svRoot!!.findViewById<EditText>(R.id.etBody) as EditText
etUrl = svRoot!!.findViewById<EditText>(R.id.etUrl) as EditText
spType = svRoot!!.findViewById<Spinner>(R.id.spType) as Spinner
ibAddHeader = svRoot!!.findViewById<ImageButton>(R.id.ibAddHeader) as ImageButton
llHeaders = svRoot!!.findViewById<LinearLayout>(R.id.llHeaders) as LinearLayout
spType!!.adapter = ArrayAdapter<Request.Type>(activity, android.R.layout.simple_list_item_1, Request.Type.values())
ibAddHeader!!.setOnClickListener({ addHeaderView() })
populateUI()
return svRoot
}
fun populateUI() {
val parent = activity as NewRequestActivity
val request = parent.getRequest()
if(request != null) {
setRequest(request)
}
}
fun addHeaderView(key: String? = null, value: String? = null) {
val inflater = activity.layoutInflater
val headerLayout = inflater!!.inflate(R.layout.layout_header_request, llHeaders) as LinearLayout
val keyLayout = headerLayout.findViewById<EditText>(R.id.etKey)
val valueLayout = headerLayout.findViewById<EditText>(R.id.etValue)
val ibRemove = headerLayout.findViewById<ImageButton>(R.id.ibRemoveHeader) as ImageButton
keyLayout.setText(key)
valueLayout.setText(value)
ibRemove.setOnClickListener({
llHeaders!!.removeView(headerLayout)
})
}
fun getHeaders(requestId: String): List<Header> {
val headers = ArrayList<Header>()
for(i in 0 until llHeaders!!.childCount) {
val headerLayout = llHeaders!!.getChildAt(i) as LinearLayout
val etKey = headerLayout.findViewById<EditText>(R.id.etKey) as EditText
val etValue = headerLayout.findViewById<EditText>(R.id.etValue) as EditText
headers.add(Header(Utility.generateId(), requestId, Header.Type.Request, etKey.text.toString(), etValue.text.toString()))
}
return headers
}
fun getRequest(): Request {
val requestId = Utility.generateId()
val headers = getHeaders(requestId)
val requestType = Request.Type.valueOf(spType!!.selectedItem.toString())
var url = etUrl!!.text.toString()
if(!url.startsWith("http")) {
url = "http://" + url
}
return Request(requestId, null, url, null, requestType, etBody!!.text.toString(), Date(), null, headers)
}
fun getSelectedTypeIndex(typeString: String): Int {
when(typeString) {
"GET" -> return 0
"POST" -> return 1
"PUT" -> return 2
"DELETE" -> return 3
}
return 0
}
fun getSelectedTypeFromIndex(type: Int): Request.Type {
when(type) {
0 -> return Request.Type.GET
1 -> return Request.Type.POST
2 -> return Request.Type.PUT
3 -> return Request.Type.DELETE
}
return Request.Type.GET
}
fun setRequest(request: Request) {
val selectedTypeIndex = getSelectedTypeIndex(spType!!.selectedItem.toString())
spType!!.setSelection(selectedTypeIndex)
etUrl!!.setText(request.url)
etBody!!.setText(request.body)
for(header in request.headers) {
addHeaderView(header.key, header.value)
}
}
fun setRequest(request: HistoryRequest) {
val selectedTypeIndex = getSelectedTypeIndex(spType!!.selectedItem.toString())
spType!!.setSelection(selectedTypeIndex)
etUrl!!.setText(request.url)
etBody!!.setText(request.body)
for(header in request.headers) {
addHeaderView(header.key, header.value)
}
}
override fun onAttach(context: Context?) {
super.onAttach(context)
if (context is OnSendRequestListener) {
mSendRequestListener = context
}
else {
throw RuntimeException(context!!.toString() + " must implement OnFragmentInteractionListener")
}
}
override fun onDetach() {
super.onDetach()
mSendRequestListener = null
}
interface OnSendRequestListener {
fun onSendRequest(request: Request)
}
companion object {
private val ARG_SECTION_NUMBER = "section_number"
fun newInstance(sectionNumber: Int): NewRequestFragment {
val fragment = NewRequestFragment()
val args = Bundle()
args.putInt(ARG_SECTION_NUMBER, sectionNumber)
fragment.arguments = args
return fragment
}
}
}
| apache-2.0 | a4be8ad519ebc4b5e9327e745a2760b9 | 32 | 133 | 0.657416 | 4.566642 | false | false | false | false |
exponent/exponent | android/expoview/src/main/java/host/exp/exponent/network/ExponentHttpClient.kt | 2 | 7412 | // Copyright 2015-present 650 Industries. All rights reserved.
package host.exp.exponent.network
import android.content.Context
import host.exp.exponent.Constants
import host.exp.exponent.analytics.Analytics
import host.exp.exponent.analytics.EXL
import okhttp3.*
import okio.BufferedSource
import okio.Okio
import org.json.JSONException
import org.json.JSONObject
import java.io.FileNotFoundException
import java.io.IOException
import java.net.MalformedURLException
import java.net.URI
import java.net.URISyntaxException
import java.net.URL
class ExponentHttpClient(
private val context: Context,
private val okHttpClientFactory: ExponentNetwork.OkHttpClientFactory
) {
interface SafeCallback {
fun onFailure(e: IOException)
fun onResponse(response: ExpoResponse)
fun onCachedResponse(response: ExpoResponse, isEmbedded: Boolean)
}
fun call(request: Request, callback: ExpoHttpCallback) {
okHttpClientFactory.getNewClient().newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
callback.onFailure(e)
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
callback.onResponse(OkHttpV1ExpoResponse(response))
}
})
}
fun callSafe(request: Request, callback: SafeCallback) {
val uri = request.url().toString()
okHttpClientFactory.getNewClient().newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
tryForcedCachedResponse(uri, request, callback, null, e)
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
callback.onResponse(OkHttpV1ExpoResponse(response))
} else {
tryForcedCachedResponse(uri, request, callback, response, null)
}
}
})
}
fun callDefaultCache(request: Request, callback: SafeCallback) {
tryForcedCachedResponse(
request.url().toString(),
request,
object : SafeCallback {
override fun onFailure(e: IOException) {
call(
request,
object : ExpoHttpCallback {
override fun onFailure(e: IOException) {
callback.onFailure(e)
}
@Throws(IOException::class)
override fun onResponse(response: ExpoResponse) {
callback.onResponse(response)
}
}
)
}
override fun onResponse(response: ExpoResponse) {
callback.onResponse(response)
}
override fun onCachedResponse(response: ExpoResponse, isEmbedded: Boolean) {
callback.onCachedResponse(response, isEmbedded)
// You are responsible for updating the cache!
}
},
null,
null
)
}
fun tryForcedCachedResponse(
uri: String,
request: Request,
callback: SafeCallback,
initialResponse: Response?,
initialException: IOException?
) {
val newRequest = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.header(ExponentNetwork.IGNORE_INTERCEPTORS_HEADER, "blah")
.build()
okHttpClientFactory.getNewClient().newCall(newRequest).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
tryHardCodedResponse(uri, call, callback, initialResponse, initialException)
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
callback.onCachedResponse(OkHttpV1ExpoResponse(response), false)
logEventWithUri(Analytics.AnalyticsEvent.HTTP_USED_CACHE_RESPONSE, uri)
} else {
tryHardCodedResponse(uri, call, callback, initialResponse, initialException)
}
}
})
}
private fun tryHardCodedResponse(
uri: String,
call: Call,
callback: SafeCallback,
initialResponse: Response?,
initialException: IOException?
) {
try {
val normalizedUri = normalizeUri(uri)
for (embeddedResponse in Constants.EMBEDDED_RESPONSES) {
// We only want to use embedded responses once. After they are used they will be added
// to the OkHttp cache and we should use the version from that cache. We don't want a situation
// where we have version 1 of a manifest saved as the embedded response, get version 2 saved
// to the OkHttp cache, cache gets evicted, and we regress to version 1. Want to only use
// monotonically increasing manifest versions.
if (normalizedUri == normalizeUri(embeddedResponse.url)) {
val response = Response.Builder()
.request(call.request())
.protocol(Protocol.HTTP_1_1)
.code(200)
.message("OK")
.body(
responseBodyForFile(
embeddedResponse.responseFilePath,
MediaType.parse(embeddedResponse.mediaType)
)
)
.build()
callback.onCachedResponse(OkHttpV1ExpoResponse(response), true)
logEventWithUri(Analytics.AnalyticsEvent.HTTP_USED_EMBEDDED_RESPONSE, uri)
return
}
}
} catch (e: Throwable) {
EXL.e(TAG, e)
}
when {
initialResponse != null -> callback.onResponse(OkHttpV1ExpoResponse(initialResponse))
initialException != null -> callback.onFailure(initialException)
else -> callback.onFailure(IOException("No hard coded response found"))
}
}
private fun responseBodyForFile(assetsPath: String, contentType: MediaType?): ResponseBody? {
return try {
var strippedAssetsPath = assetsPath
if (strippedAssetsPath.startsWith("assets://")) {
strippedAssetsPath = strippedAssetsPath.substring("assets://".length)
}
val stream = context.assets.open(strippedAssetsPath)
val source = Okio.source(stream)
val buffer = Okio.buffer(source)
object : ResponseBody() {
override fun contentType(): MediaType? {
return contentType
}
override fun contentLength(): Long {
return -1
}
override fun source(): BufferedSource {
return buffer
}
}
} catch (e: FileNotFoundException) {
EXL.e(TAG, e)
null
} catch (e: IOException) {
EXL.e(TAG, e)
null
}
}
private fun logEventWithUri(event: Analytics.AnalyticsEvent, uri: String) {
try {
val eventProperties = JSONObject().apply {
put("URI", uri)
}
Analytics.logEvent(event, eventProperties)
} catch (e: JSONException) {
EXL.e(TAG, e)
}
}
companion object {
private val TAG = ExponentHttpClient::class.java.simpleName
private fun normalizeUri(uriString: String): String {
return try {
val url = URL(uriString)
var port = url.port
if (port == -1) {
if (url.protocol == "http") {
port = 80
} else if (url.protocol == "https") {
port = 443
}
}
val uri = URI(url.protocol, url.userInfo, url.host, port, url.path, url.query, url.ref)
uri.toString()
} catch (e: MalformedURLException) {
uriString
} catch (e: URISyntaxException) {
uriString
}
}
}
}
| bsd-3-clause | ef43fc6b78e4b05921cfcac07f36b5f4 | 30.40678 | 103 | 0.640313 | 4.564039 | false | false | false | false |
exponent/exponent | android/expoview/src/main/java/host/exp/exponent/kernel/services/sensors/SubscribableSensorKernelService.kt | 2 | 6347 | // Copyright 2015-present 650 Industries. All rights reserved.
package host.exp.exponent.kernel.services.sensors
import android.content.Context
import android.hardware.SensorEvent
import host.exp.exponent.kernel.ExperienceKey
import java.lang.ref.WeakReference
import java.util.*
private var DEFAULT_UPDATE_INTERVAL = 100
abstract class SubscribableSensorKernelService internal constructor(reactContext: Context) : BaseSensorKernelService(reactContext) {
private val experienceScopeKeyListenersCountMap: MutableMap<String?, Int> = mutableMapOf()
private val sensorEventListenerLastUpdateMap: MutableMap<SensorKernelServiceSubscription, Long> = WeakHashMap()
private val experienceScopeKeySubscriptionsMap: MutableMap<String?, MutableList<WeakReference<SensorKernelServiceSubscription?>>> = mutableMapOf()
override fun onExperienceForegrounded(experienceKey: ExperienceKey) {
updateObserving()
}
override fun onExperienceBackgrounded(experienceKey: ExperienceKey) {
updateObserving()
}
override fun onSensorDataChanged(sensorEvent: SensorEvent) {
val currentTime = System.currentTimeMillis()
val listeners = experienceScopeKeySubscriptionsMap[currentExperienceKey!!.scopeKey]
if (listeners != null) {
for (weakReference in listeners) {
val sensorKernelServiceSubscription = weakReference.get()
if (sensorKernelServiceSubscription != null && sensorKernelServiceSubscription.isEnabled) {
var lastUpdate: Long = 0
if (sensorEventListenerLastUpdateMap.containsKey(sensorKernelServiceSubscription)) {
lastUpdate = sensorEventListenerLastUpdateMap[sensorKernelServiceSubscription]!!
}
var updateInterval = DEFAULT_UPDATE_INTERVAL.toLong()
if (sensorKernelServiceSubscription.updateInterval != null) {
updateInterval = sensorKernelServiceSubscription.updateInterval!!
}
if (currentTime - lastUpdate > updateInterval) {
sensorKernelServiceSubscription.sensorEventListener.onSensorDataChanged(sensorEvent)
sensorEventListenerLastUpdateMap[sensorKernelServiceSubscription] = currentTime
}
}
}
}
}
// Modules API
fun createSubscriptionForListener(
experienceKey: ExperienceKey,
listener: SensorEventListener
): SensorKernelServiceSubscription {
val sensorKernelServiceSubscription =
SensorKernelServiceSubscription(experienceKey, this, listener)
if (!experienceScopeKeySubscriptionsMap.containsKey(experienceKey.scopeKey)) {
experienceScopeKeySubscriptionsMap[experienceKey.scopeKey] = ArrayList()
}
experienceScopeKeySubscriptionsMap[experienceKey.scopeKey]!!.add(
WeakReference(
sensorKernelServiceSubscription
)
)
return sensorKernelServiceSubscription
}
fun removeSubscription(subscriptionToRemove: SensorKernelServiceSubscription) {
sensorEventListenerLastUpdateMap.remove(subscriptionToRemove)
val experienceKey = subscriptionToRemove.experienceKey
if (experienceScopeKeySubscriptionsMap.containsKey(experienceKey.scopeKey)) {
val originalSubscriptions: List<WeakReference<SensorKernelServiceSubscription?>> =
experienceScopeKeySubscriptionsMap[experienceKey.scopeKey]!!
val leftSubscriptions: MutableList<WeakReference<SensorKernelServiceSubscription?>> =
ArrayList()
for (subscriptionWeakReference in originalSubscriptions) {
val subscription = subscriptionWeakReference.get()
if (subscription != null && subscription != subscriptionToRemove) {
leftSubscriptions.add(subscriptionWeakReference)
}
}
if (leftSubscriptions.size > 0) {
experienceScopeKeySubscriptionsMap[experienceKey.scopeKey] = leftSubscriptions
} else {
experienceScopeKeySubscriptionsMap.remove(experienceKey.scopeKey)
}
}
}
// SensorKernelServiceSubscription API
fun onSubscriptionEnabledChanged(sensorKernelServiceSubscription: SensorKernelServiceSubscription) {
val experienceKey = sensorKernelServiceSubscription.experienceKey
val enabledListenersCount = getEnabledListenersForExperienceKey(experienceKey)
if (sensorKernelServiceSubscription.isEnabled) {
experienceScopeKeyListenersCountMap[experienceKey.scopeKey] = enabledListenersCount + 1
} else {
experienceScopeKeyListenersCountMap[experienceKey.scopeKey] = enabledListenersCount - 1
}
if (getEnabledListenersForExperienceKey(experienceKey) == 0) {
experienceScopeKeyListenersCountMap.remove(experienceKey.scopeKey)
}
updateObserving()
}
// android.hardware.SensorEventListener
override fun onSensorChanged(sensorEvent: SensorEvent) {
if (sensorEvent.sensor.type == sensorType) {
onSensorDataChanged(sensorEvent)
}
}
// Private helpers
private fun getEnabledListenersForExperienceKey(experienceKey: ExperienceKey?): Int {
// null is an expected key, or at least that's how the code was written. may be a bug.
val mapKey = experienceKey?.scopeKey
return if (experienceScopeKeyListenersCountMap.containsKey(mapKey)) {
experienceScopeKeyListenersCountMap[mapKey]!!
} else 0
}
private fun cleanWeakSubscriptionsList(experienceKey: ExperienceKey?) {
// null is an expected key, or at least that's how the code was written. may be a bug.
val mapKey = experienceKey?.scopeKey
val listeners: List<WeakReference<SensorKernelServiceSubscription?>>? =
experienceScopeKeySubscriptionsMap[mapKey]
val realListeners: MutableList<WeakReference<SensorKernelServiceSubscription?>> = ArrayList()
if (listeners != null) {
for (subscriptionWeakReference in listeners) {
if (subscriptionWeakReference.get() != null) {
realListeners.add(subscriptionWeakReference)
}
}
}
if (realListeners.size > 0) {
experienceScopeKeySubscriptionsMap[mapKey] = realListeners
} else {
experienceScopeKeySubscriptionsMap.remove(mapKey)
}
}
private fun updateObserving() {
cleanWeakSubscriptionsList(currentExperienceKey)
// Start/stop observing according to the experience state
if (getEnabledListenersForExperienceKey(currentExperienceKey) > 0) {
super.startObserving()
} else {
super.stopObserving()
}
}
}
| bsd-3-clause | 9c84f873b378e4254e7badd92724d710 | 41.313333 | 148 | 0.756578 | 5.764759 | false | false | false | false |
Undin/intellij-rust | src/main/kotlin/org/rust/lang/core/psi/ext/RsTypeParameter.kt | 2 | 2621 | /*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.lang.core.psi.ext
import com.intellij.lang.ASTNode
import com.intellij.psi.search.SearchScope
import com.intellij.psi.stubs.IStubElementType
import org.rust.lang.core.psi.RsPathType
import org.rust.lang.core.psi.RsPolybound
import org.rust.lang.core.psi.RsPsiImplUtil
import org.rust.lang.core.psi.RsTypeParameter
import org.rust.lang.core.stubs.RsTypeParameterStub
import org.rust.lang.core.types.RsPsiTypeImplUtil
import org.rust.lang.core.types.ty.Ty
val RsTypeParameter.owner: RsGenericDeclaration?
get() = parent?.parent as? RsGenericDeclaration
/**
* Returns all bounds for type parameter.
*
* Don't use it for stub creation because it will cause [com.intellij.openapi.project.IndexNotReadyException]!
*/
val RsTypeParameter.bounds: List<RsPolybound> get() {
val whereBounds =
owner?.whereClause?.wherePredList.orEmpty()
.filter { (it.typeReference?.skipParens() as? RsPathType)?.path?.reference?.resolve() == this }
.flatMap { it.typeParamBounds?.polyboundList.orEmpty() }
return typeParamBounds?.polyboundList.orEmpty() + whereBounds
}
/**
* Note that a type parameter can be sized or not sized in different contexts:
*
* ```
* impl<T: ?Sized> Box<T> {
* fn foo() -> Box<T> { unimplemented!() }
* //^ `T` is NOT sized
* fn foo() -> Box<T> where T: Sized { unimplemented!() }
* } //^ `T` is sized
* ```
*/
val RsTypeParameter.isSized: Boolean
get() {
// We just check `?` before trait name in bound because at this moment only `Sized` trait can have `?` modifier
val owner = parent?.parent as? RsGenericDeclaration
val whereBounds =
owner?.whereClause?.wherePredList.orEmpty()
.filter { (it.typeReference?.skipParens() as? RsPathType)?.path?.referenceName == name }
.flatMap { it.typeParamBounds?.polyboundList.orEmpty() }
val bounds = typeParamBounds?.polyboundList.orEmpty() + whereBounds
return bounds.none { it.hasQ }
}
abstract class RsTypeParameterImplMixin : RsStubbedNamedElementImpl<RsTypeParameterStub>, RsTypeParameter {
constructor(node: ASTNode) : super(node)
constructor(stub: RsTypeParameterStub, nodeType: IStubElementType<*, *>) : super(stub, nodeType)
override val declaredType: Ty get() = RsPsiTypeImplUtil.declaredType(this)
override fun getUseScope(): SearchScope = RsPsiImplUtil.getParameterUseScope(this) ?: super.getUseScope()
}
| mit | 1ab35afa4f8fb4d7a21ac2043841a76b | 38.712121 | 119 | 0.693628 | 4.121069 | false | false | false | false |
b005t3r/Kotling | core/src/main/kotlin/com/kotling/rendering/VertexFormat.kt | 1 | 3677 | package com.kotling.rendering
import com.badlogic.gdx.graphics.VertexAttribute
import com.badlogic.gdx.graphics.VertexAttributes
import kotlin.reflect.KProperty
@Target(AnnotationTarget.PROPERTY)
annotation class VertexFormat(vararg val format:String = arrayOf("position:float2"))
object VertexAttributesCache {
private val componentCountPerType = mutableMapOf("float1" to 1, "float2" to 2, "float3" to 3, "float4" to 4, "byte4" to 4)
private val attributes:MutableMap<Array<out String>, VertexAttributes> = mutableMapOf()
fun fromAnnotation(format:VertexFormat):VertexAttributes = fromString(*format.format)
fun fromString(vararg format:String):VertexAttributes {
if(format.size == 0)
throw IllegalArgumentException("invalid format: $format")
var attrs:VertexAttributes? = attributes[format]
if(attrs != null)
return attrs
attrs = create(*format)
attributes[format] = attrs
return attrs
}
operator fun getValue(thisRef:Any?, property:KProperty<*>):VertexAttributes {
val vertexFormat:VertexFormat = property.annotations.find { it is VertexFormat } as VertexFormat? ?: throw UninitializedPropertyAccessException("property not annotated with @VertexFormat")
return VertexAttributesCache.fromAnnotation(vertexFormat)
}
private fun create(vararg format:String):VertexAttributes {
var hasPosition = false
var attrs = Array(
format.size,
fun(i:Int):VertexAttribute {
val parts = format[i].split(":")
val name = parts[0]
val type = parts[1]
val usage = getUsage(name, type)
if(! hasPosition && usage == VertexAttributes.Usage.Position)
hasPosition = true
return when {
usage == VertexAttributes.Usage.ColorPacked && type != "byte4" ->
throw IllegalArgumentException("invalid type for $name: packed colors have to use 'byte4' type")
usage == VertexAttributes.Usage.ColorUnpacked && type != "float4" ->
throw IllegalArgumentException("invalid type for $name: unpacked colors have to use 'float4' type")
usage == VertexAttributes.Usage.TextureCoordinates && type != "float2" ->
throw IllegalArgumentException("invalid type for $name: texture coordinates have to use 'float2' type")
usage == VertexAttributes.Usage.Position && type != "float2" ->
throw IllegalArgumentException("invalid type for $name: positions have to use 'float2' type")
else -> VertexAttribute(usage, getComponentCount(type), name)
}
}
);
if(! hasPosition)
throw IllegalArgumentException("position attribute not present in $format")
return VertexAttributes(*attrs)
}
private fun getUsage(name:String, type:String):Int = when {
name.contains("position", true) -> VertexAttributes.Usage.Position
name.contains("color", true) && type == "byte4" -> VertexAttributes.Usage.ColorPacked
name.contains("color", true) && type == "float4" -> VertexAttributes.Usage.ColorUnpacked
name.contains("uv", true) || name.contains("texCoord", true) -> VertexAttributes.Usage.TextureCoordinates
else -> VertexAttributes.Usage.Generic
}
private fun getComponentCount(type:String):Int = componentCountPerType[type] ?: throw IllegalArgumentException("invalid type: $type, allowed types are ${componentCountPerType.keys}")
}
| mit | 02c0431d2e02e99c32b7788d9195d903 | 42.77381 | 196 | 0.651346 | 4.928954 | false | false | false | false |
Undin/intellij-rust | src/main/kotlin/org/rust/ide/inspections/fixes/RemoveParameterFix.kt | 2 | 2116 | /*
* Use of this source code is governed by the MIT license that can be
* found in the LICENSE file.
*/
package org.rust.ide.inspections.fixes
import com.intellij.codeInspection.LocalQuickFixOnPsiElement
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.parentOfType
import org.rust.lang.core.psi.*
import org.rust.lang.core.psi.ext.*
/**
* Fix that removes a parameter and all its usages at call sites.
*/
class RemoveParameterFix(binding: RsPatBinding, private val bindingName: String) : LocalQuickFixOnPsiElement(binding) {
override fun getText() = "Remove parameter `${bindingName}`"
override fun getFamilyName() = "Remove parameter"
override fun invoke(project: Project, file: PsiFile, startElement: PsiElement, endElement: PsiElement) {
val binding = startElement as? RsPatBinding ?: return
val patIdent = binding.topLevelPattern as? RsPatIdent ?: return
val parameter = patIdent.parent as? RsValueParameter ?: return
val function = parameter.parentOfType<RsFunction>() ?: return
val parameterIndex = function.valueParameterList?.valueParameterList?.indexOf(parameter) ?: -1
if (parameterIndex == -1) return
parameter.deleteWithSurroundingCommaAndWhitespace()
removeArguments(function, parameterIndex)
}
}
private fun removeArguments(function: RsFunction, parameterIndex: Int) {
if (function.isIntentionPreviewElement) return
val calls = function.findFunctionCalls() + function.findMethodCalls()
calls.forEach { call ->
val arguments = when (call) {
is RsCallExpr -> call.valueArgumentList
is RsMethodCall -> call.valueArgumentList
else -> return@forEach
}
val isMethod = function.hasSelfParameters
val argumentIndex = when {
isMethod && call is RsCallExpr -> parameterIndex + 1 // UFCS
else -> parameterIndex
}
arguments.exprList.getOrNull(argumentIndex)?.deleteWithSurroundingCommaAndWhitespace()
}
}
| mit | 834ec24fa2497acc1e5e95167b688874 | 38.185185 | 119 | 0.715028 | 4.853211 | false | false | false | false |
minecraft-dev/MinecraftDev | src/main/kotlin/facet/MinecraftFacetDetector.kt | 1 | 8368 | /*
* Minecraft Dev for IntelliJ
*
* https://minecraftdev.org
*
* Copyright (c) 2022 minecraft-dev
*
* MIT License
*/
package com.demonwav.mcdev.facet
import com.demonwav.mcdev.platform.PlatformType
import com.demonwav.mcdev.platform.architectury.framework.ARCHITECTURY_LIBRARY_KIND
import com.demonwav.mcdev.platform.architectury.framework.ArchitecturyGradleData
import com.demonwav.mcdev.platform.fabric.framework.FABRIC_LIBRARY_KIND
import com.demonwav.mcdev.platform.mcp.gradle.tooling.archloom.ArchitecturyModel
import com.demonwav.mcdev.platform.sponge.framework.SPONGE_LIBRARY_KIND
import com.demonwav.mcdev.util.ifEmpty
import com.demonwav.mcdev.util.runWriteTaskLater
import com.intellij.facet.FacetManager
import com.intellij.facet.impl.ui.libraries.LibrariesValidatorContextImpl
import com.intellij.framework.library.LibraryVersionProperties
import com.intellij.openapi.module.Module
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootEvent
import com.intellij.openapi.roots.ModuleRootListener
import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.roots.libraries.LibraryDetectionManager
import com.intellij.openapi.roots.libraries.LibraryKind
import com.intellij.openapi.roots.libraries.LibraryProperties
import com.intellij.openapi.roots.ui.configuration.libraries.LibraryPresentationManager
import com.intellij.openapi.startup.StartupActivity
import com.intellij.openapi.util.Key
import org.jetbrains.plugins.gradle.util.GradleUtil
class MinecraftFacetDetector : StartupActivity {
companion object {
private val libraryVersionsKey = Key<MutableMap<LibraryKind, String>>("mcdev.libraryVersions")
fun getLibraryVersions(module: Module): Map<LibraryKind, String> {
return module.getUserData(libraryVersionsKey) ?: emptyMap()
}
}
override fun runActivity(project: Project) {
MinecraftModuleRootListener.doCheck(project)
}
private object MinecraftModuleRootListener : ModuleRootListener {
override fun rootsChanged(event: ModuleRootEvent) {
if (event.isCausedByFileTypesChange) {
return
}
val project = event.source as? Project ?: return
doCheck(project)
}
fun doCheck(project: Project) {
val moduleManager = ModuleManager.getInstance(project)
for (module in moduleManager.modules) {
val facetManager = FacetManager.getInstance(module)
val minecraftFacet = facetManager.getFacetByType(MinecraftFacet.ID)
if (minecraftFacet == null) {
checkNoFacet(module)
} else {
checkExistingFacet(module, minecraftFacet)
}
}
}
private fun checkNoFacet(module: Module) {
val platforms = autoDetectTypes(module).ifEmpty { return }
val facetManager = FacetManager.getInstance(module)
val configuration = MinecraftFacetConfiguration()
configuration.state.autoDetectTypes.addAll(platforms)
val facetType = MinecraftFacet.facetTypeOrNull ?: return
val facet = facetManager.createFacet(facetType, "Minecraft", configuration, null)
runWriteTaskLater {
// Only add the new facet if there isn't a Minecraft facet already - double check here since this
// task may run much later
if (module.isDisposed || facet.isDisposed) {
// Module may be disposed before we run
return@runWriteTaskLater
}
if (facetManager.getFacetByType(MinecraftFacet.ID) == null) {
val model = facetManager.createModifiableModel()
model.addFacet(facet)
model.commit()
}
}
}
private fun checkExistingFacet(module: Module, facet: MinecraftFacet) {
val platforms = autoDetectTypes(module).ifEmpty { return }
val types = facet.configuration.state.autoDetectTypes
types.clear()
types.addAll(platforms)
if (facet.configuration.state.forgePatcher) {
// make sure Forge and MCP are present
types.add(PlatformType.FORGE)
types.add(PlatformType.MCP)
}
facet.refresh()
}
private fun autoDetectTypes(module: Module): Set<PlatformType> {
val libraryVersions = module.getUserData(libraryVersionsKey)
?: mutableMapOf<LibraryKind, String>().also { module.putUserData(libraryVersionsKey, it) }
libraryVersions.clear()
val presentationManager = LibraryPresentationManager.getInstance()
val context = LibrariesValidatorContextImpl(module)
val platformKinds = mutableSetOf<LibraryKind>()
context.rootModel
.orderEntries()
.using(context.modulesProvider)
.recursively()
.librariesOnly()
.forEachLibrary forEach@{ library ->
MINECRAFT_LIBRARY_KINDS.forEach { kind ->
if (presentationManager.isLibraryOfKind(library, context.librariesContainer, setOf(kind))) {
val libraryFiles =
context.librariesContainer.getLibraryFiles(library, OrderRootType.CLASSES).toList()
LibraryDetectionManager.getInstance().processProperties(
libraryFiles,
object : LibraryDetectionManager.LibraryPropertiesProcessor {
override fun <P : LibraryProperties<*>> processProperties(
kind: LibraryKind,
properties: P
): Boolean {
return if (properties is LibraryVersionProperties) {
libraryVersions[kind] = properties.versionString ?: return true
false
} else {
true
}
}
}
)
platformKinds.add(kind)
}
}
return@forEach true
}
context.rootModel
.orderEntries()
.using(context.modulesProvider)
.recursively()
.withoutLibraries()
.withoutSdk()
.forEachModule forEach@{ m ->
if (m.name.startsWith("SpongeAPI", ignoreCase = true)) {
// We don't want want to add parent modules in module groups
val moduleManager = ModuleManager.getInstance(m.project)
val groupPath = moduleManager.getModuleGroupPath(m)
if (groupPath == null) {
platformKinds.add(SPONGE_LIBRARY_KIND)
return@forEach true
}
val name = groupPath.lastOrNull() ?: return@forEach true
if (m.name == name) {
return@forEach true
}
platformKinds.add(SPONGE_LIBRARY_KIND)
}
return@forEach true
}
val architecturyGradleData = GradleUtil.findGradleModuleData(module)?.children
?.find { it.key == ArchitecturyGradleData.KEY }?.data as? ArchitecturyGradleData
if (architecturyGradleData?.moduleType == ArchitecturyModel.ModuleType.COMMON) {
platformKinds.add(ARCHITECTURY_LIBRARY_KIND)
platformKinds.removeIf { it == FABRIC_LIBRARY_KIND }
}
return platformKinds.mapNotNull { kind -> PlatformType.fromLibraryKind(kind) }.toSet()
}
}
}
| mit | adc45606ffc36f576fcf4ffa9327834e | 42.811518 | 116 | 0.583891 | 5.692517 | false | false | false | false |
andstatus/andstatus | app/src/main/kotlin/org/andstatus/app/service/CommandResult.kt | 1 | 10508 | /*
* Copyright (C) 2014-2017 yvolk (Yuri Volkov), http://yurivolkov.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.andstatus.app.service
import android.content.ContentValues
import android.database.Cursor
import android.os.Parcel
import android.os.Parcelable
import org.andstatus.app.context.MyContextHolder
import org.andstatus.app.data.DbUtils
import org.andstatus.app.database.table.CommandTable
import org.andstatus.app.notification.NotificationEventType
import org.andstatus.app.util.MyStringBuilder
import org.andstatus.app.util.RelativeTime
import org.andstatus.app.util.RelativeTime.millisToDelaySeconds
import java.util.*
import java.util.concurrent.atomic.AtomicLong
import java.util.function.Consumer
/**
* Result of the command execution
* See also [android.content.SyncStats]
* @author [email protected]
*/
class CommandResult : Parcelable {
var delayedTill: Long? = null
set(value) {
field = value?.let { if (it > 0) it else null }
}
private var lastExecutedDate: Long = 0
private var executionCount = 0
private var retriesLeft = 0
private var executed = false
private var numAuthExceptions: Long = 0
private var numIoExceptions: Long = 0
private var numParseExceptions: Long = 0
private var mMessage: String = ""
private var progress: String = ""
private var itemId: Long = 0
// 0 means these values were not set
private var hourlyLimit = 0
private var remainingHits = 0
// Counters for user notifications
private var downloadedCount: Long = 0
private var newCount: Long = 0
val notificationEventCounts: MutableMap<NotificationEventType, AtomicLong> = HashMap()
constructor() {}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeLong(delayedTill ?: 0)
dest.writeLong(lastExecutedDate)
dest.writeInt(executionCount)
dest.writeInt(retriesLeft)
dest.writeLong(numAuthExceptions)
dest.writeLong(numIoExceptions)
dest.writeLong(numParseExceptions)
dest.writeString(mMessage)
dest.writeLong(itemId)
dest.writeInt(hourlyLimit)
dest.writeInt(remainingHits)
dest.writeLong(downloadedCount)
dest.writeString(progress)
}
constructor(parcel: Parcel) {
delayedTill = parcel.readLong()
lastExecutedDate = parcel.readLong()
executionCount = parcel.readInt()
retriesLeft = parcel.readInt()
numAuthExceptions = parcel.readLong()
numIoExceptions = parcel.readLong()
numParseExceptions = parcel.readLong()
mMessage = parcel.readString() ?: ""
itemId = parcel.readLong()
hourlyLimit = parcel.readInt()
remainingHits = parcel.readInt()
downloadedCount = parcel.readLong()
progress = parcel.readString() ?: ""
}
fun toContentValues(values: ContentValues) {
delayedTill // TODO
values.put(CommandTable.LAST_EXECUTED_DATE, lastExecutedDate)
values.put(CommandTable.EXECUTION_COUNT, executionCount)
values.put(CommandTable.RETRIES_LEFT, retriesLeft)
values.put(CommandTable.NUM_AUTH_EXCEPTIONS, numAuthExceptions)
values.put(CommandTable.NUM_IO_EXCEPTIONS, numIoExceptions)
values.put(CommandTable.NUM_PARSE_EXCEPTIONS, numParseExceptions)
values.put(CommandTable.ERROR_MESSAGE, mMessage)
values.put(CommandTable.DOWNLOADED_COUNT, downloadedCount)
values.put(CommandTable.PROGRESS_TEXT, progress)
}
fun getExecutionCount(): Int {
return executionCount
}
fun hasError(): Boolean {
return hasSoftError() || hasHardError()
}
fun hasHardError(): Boolean {
return numAuthExceptions > 0 || numParseExceptions > 0
}
fun hasSoftError(): Boolean {
return numIoExceptions > 0
}
override fun describeContents(): Int {
return 0
}
fun setSoftErrorIfNotOk(ok: Boolean) {
if (!ok) {
incrementNumIoExceptions()
}
}
override fun toString(): String {
return MyStringBuilder.formatKeyValue(this, toSummaryBuilder())
}
fun toSummary(): String {
return toSummaryBuilder().toString()
}
private fun toSummaryBuilder(): MyStringBuilder {
val sb = MyStringBuilder()
delayedTill.millisToDelaySeconds()?.let { sb.withComma("Delayed for $it sec") }
if (executionCount > 0) {
sb.withComma("executed", executionCount)
sb.withComma(
"last", RelativeTime.getDifference(
MyContextHolder.myContextHolder.getNow().context, lastExecutedDate
)
)
if (retriesLeft > 0) sb.withComma("retriesLeft", retriesLeft)
if (!hasError()) sb.withComma("error", "none")
}
if (hasError()) sb.withComma("error", if (hasHardError()) "Hard" else "Soft")
if (downloadedCount > 0) sb.withComma("downloaded", downloadedCount)
if (newCount > 0) sb.withComma("new", newCount)
notificationEventCounts.forEach { event: NotificationEventType, count: AtomicLong ->
if (count.get() > 0) sb.withComma(event.name, count.get())
}
if (mMessage.isNotEmpty()) sb.append("\n$mMessage")
return sb
}
fun getNumAuthExceptions(): Long {
return numAuthExceptions
}
protected fun incrementNumAuthExceptions() {
numAuthExceptions++
}
fun getNumIoExceptions(): Long {
return numIoExceptions
}
fun incrementNumIoExceptions() {
numIoExceptions++
}
fun getNumParseExceptions(): Long {
return numParseExceptions
}
fun incrementParseExceptions() {
numParseExceptions++
}
fun getHourlyLimit(): Int {
return hourlyLimit
}
fun setHourlyLimit(hourlyLimit: Int) {
this.hourlyLimit = hourlyLimit
}
fun getRemainingHits(): Int {
return remainingHits
}
fun setRemainingHits(remainingHits: Int) {
this.remainingHits = remainingHits
}
fun incrementNewCount() {
newCount++
}
fun onNotificationEvent(event: NotificationEventType) {
if (event == NotificationEventType.EMPTY) return
val count = notificationEventCounts.get(event)
count?.incrementAndGet()
if (count == null) notificationEventCounts[event] = AtomicLong(1)
}
fun incrementDownloadedCount() {
downloadedCount++
}
fun getDownloadedCount(): Long {
return downloadedCount
}
fun getNewCount(): Long {
return newCount
}
fun getRetriesLeft(): Int {
return retriesLeft
}
fun resetRetries(command: CommandEnum?) {
retriesLeft = INITIAL_NUMBER_OF_RETRIES
when (command) {
CommandEnum.GET_TIMELINE, CommandEnum.GET_OLDER_TIMELINE, CommandEnum.RATE_LIMIT_STATUS -> retriesLeft = 0
else -> {
}
}
prepareForLaunch()
}
fun prepareForLaunch() {
delayedTill = null
executed = false
numAuthExceptions = 0
numIoExceptions = 0
numParseExceptions = 0
mMessage = ""
itemId = 0
hourlyLimit = 0
remainingHits = 0
newCount = 0
notificationEventCounts.values.forEach(Consumer { c: AtomicLong -> c.set(0) })
downloadedCount = 0
progress = ""
}
fun afterExecutionEnded() {
if (delayedTill == null) {
executed = true
executionCount++
if (retriesLeft > 0) {
retriesLeft -= 1
}
lastExecutedDate = System.currentTimeMillis()
}
}
fun shouldWeRetry(): Boolean {
return (!executed || hasError()) && !hasHardError() && retriesLeft > 0
}
fun getItemId(): Long {
return itemId
}
fun setItemId(itemId: Long) {
this.itemId = itemId
}
fun getLastExecutedDate(): Long {
return lastExecutedDate
}
fun getMessage(): String {
return mMessage
}
fun setMessage(message: String) {
mMessage = message
}
fun getProgress(): String {
return progress
}
fun setProgress(progress: String) {
this.progress = progress
}
companion object {
const val INITIAL_NUMBER_OF_RETRIES = 10
@JvmField val CREATOR: Parcelable.Creator<CommandResult> = object : Parcelable.Creator<CommandResult> {
override fun createFromParcel(inp: Parcel): CommandResult {
return CommandResult(inp)
}
override fun newArray(size: Int): Array<CommandResult?> {
return arrayOfNulls<CommandResult?>(size)
}
}
fun fromCursor(cursor: Cursor): CommandResult {
val result = CommandResult()
result.delayedTill // TODO
result.lastExecutedDate = DbUtils.getLong(cursor, CommandTable.LAST_EXECUTED_DATE)
result.executionCount = DbUtils.getInt(cursor, CommandTable.EXECUTION_COUNT)
result.retriesLeft = DbUtils.getInt(cursor, CommandTable.RETRIES_LEFT)
result.numAuthExceptions = DbUtils.getLong(cursor, CommandTable.NUM_AUTH_EXCEPTIONS)
result.numIoExceptions = DbUtils.getLong(cursor, CommandTable.NUM_IO_EXCEPTIONS)
result.numParseExceptions = DbUtils.getLong(cursor, CommandTable.NUM_PARSE_EXCEPTIONS)
result.mMessage = DbUtils.getString(cursor, CommandTable.ERROR_MESSAGE)
result.downloadedCount = DbUtils.getInt(cursor, CommandTable.DOWNLOADED_COUNT).toLong()
result.progress = DbUtils.getString(cursor, CommandTable.PROGRESS_TEXT)
return result
}
fun toString(commandResult: CommandResult?): String {
return commandResult?.toString() ?: "(result is null)"
}
}
}
| apache-2.0 | f0c39ecd926ad49cca9bd7f2290e0f95 | 30.367164 | 118 | 0.645318 | 4.604733 | false | false | false | false |
CruGlobal/android-gto-support | gto-support-androidx-databinding/src/main/java/org/ccci/gto/android/common/androidx/databinding/widget/DataBindingArrayAdapter.kt | 2 | 1448 | package org.ccci.gto.android.common.androidx.databinding.widget
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
abstract class DataBindingArrayAdapter<B : ViewDataBinding, T>(
context: Context,
@LayoutRes private val layout: Int,
private val lifecycleOwner: LifecycleOwner? = null
) : ArrayAdapter<T>(context, layout), Observer<Collection<T>> {
final override fun getView(position: Int, convertView: View?, parent: ViewGroup) =
(convertView?.getBinding() ?: inflateBinding(parent))
.also { onBind(it, position) }
.root
private fun View.getBinding(): B? = DataBindingUtil.getBinding(this)
private fun inflateBinding(parent: ViewGroup): B =
DataBindingUtil.inflate<B>(LayoutInflater.from(parent.context), layout, parent, false)
.also {
it.lifecycleOwner = lifecycleOwner
onBindingCreated(it)
}
protected abstract fun onBindingCreated(binding: B)
protected abstract fun onBind(binding: B, position: Int)
override fun onChanged(t: Collection<T>?) {
clear()
if (t != null) addAll(t)
}
}
| mit | 080df37ad3d459e61f2581163c143d06 | 36.128205 | 94 | 0.718232 | 4.686084 | false | false | false | false |
Tvede-dk/CommonsenseAndroidKotlin | app/src/main/kotlin/csense/android/exampleApp/activity/MainActivity.kt | 1 | 5348 | package csense.android.exampleApp.activity
import android.annotation.*
import android.net.*
import android.support.annotation.*
import android.support.v7.widget.*
import com.commonsense.android.kotlin.base.*
import com.commonsense.android.kotlin.system.extensions.*
import com.commonsense.android.kotlin.system.imaging.*
import com.commonsense.android.kotlin.system.logging.*
import com.commonsense.android.kotlin.views.*
import com.commonsense.android.kotlin.views.databinding.activities.*
import com.commonsense.android.kotlin.views.databinding.adapters.*
import com.commonsense.android.kotlin.views.extensions.*
import csense.android.exampleApp.R
import csense.android.exampleApp.databinding.*
import csense.android.exampleApp.views.dataAware.*
import csense.android.exampleApp.views.searchable.SearchableRecyclerDemoActivity
import csense.android.exampleApp.views.tools.*
import csense.android.exampleApp.views.widgets.*
import kotlinx.coroutines.*
class MainActivity : BaseDatabindingActivity<MainActivityBinding>() {
override fun createBinding(): InflaterFunctionSimple<MainActivityBinding> =
MainActivityBinding::inflate
val adapter by lazy {
BaseDataBindingRecyclerAdapter(this)
}
override fun useBinding() {
setupAdapter()
val manager = GridLayoutManager(this, 2)
binding.mainActivityRecyclerView.setup(adapter, manager)
}
@SuppressLint("MissingPermission")
private fun setupAdapter() {
adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_tools) {
startActivity(ToolsOverviewActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_adapter) {
startActivity(WidgetsRecyclerExampleActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_base_activity) {
startActivity(ToolsOverviewActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_base_fragment) {
startActivity(ToolsOverviewActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_widgets) {
startActivity(WidgetsOverviewActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.intro_text) {
startActivity(DataAActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_searchable_recycler) {
startActivity(SearchableRecyclerDemoActivity::class)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.retrive_picture) {
PictureRetriver(
this,
{ path: Uri, fromCamera: Boolean ->
launchInUi("safeToast") {
delay(2500)
logDebug("Got image Uri = $path, was it from camera ? = $fromCamera")
it.safeToast("Got image Uri = $path, was it from camera ? = $fromCamera")
}
},
null
).getImage(true)
}, 0)
adapter.add(CategoryRecyclerRender(R.string.retrive_picture) {
PictureRetriver(
this,
{ path: Uri, fromCamera: Boolean ->
launchInUi("safeToast") {
delay(2500)
logDebug("Got image Uri = $path, was it from camera ? = $fromCamera")
it.safeToast("Got image Uri = $path, was it from camera ? = $fromCamera")
}
},
null
).getImage(false)
}, 0)
// adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_activitydata, {
// startActivity(StartActivityData::class)
// }), 0)
// adapter.add(CategoryRecyclerRender(R.string.mainactivity_category_crypto, {
// startActivity(MainCryptoActivity::class)
// }), 0)
//categories:
// 1) tools
// - crash
// - anr
// - fps
// - lifecycle events listener
// - ...
// -
// 2) views
// - base databinding examples (activity / fragments)
// - base databinding recycler adapter
// - base databinding listView
// -
// -
// 3) advanced concepts
// - activity with data
// -
logError(toPrettyString())
}
}
data class CategoryRenderData(@StringRes val text: Int, val onclick: EmptyFunction)
class CategoryRecyclerRender(@StringRes text: Int, onclick: EmptyFunction)
: BaseRenderModel<CategoryRenderData, CategoryRenderViewBinding>(CategoryRenderData(text, onclick), CategoryRenderViewBinding::class) {
override fun getInflaterFunction(): ViewInflatingFunction<CategoryRenderViewBinding> = CategoryRenderViewBinding::inflate
override fun renderFunction(view: CategoryRenderViewBinding,
model: CategoryRenderData,
viewHolder: BaseViewHolderItem<CategoryRenderViewBinding>) {
view.categoryRenderViewButton.setText(model.text)
view.categoryRenderViewButton.setOnclickAsyncEmpty(model.onclick)
}
} | mit | a2486aa9b78e0cac049b155f116c4efa | 37.76087 | 139 | 0.637809 | 4.870674 | false | false | false | false |
coder3101/gdgApp | gdg/src/main/java/com/softminds/gdg/activities/AboutUs.kt | 1 | 1969 | /*
* Copyright (c) Ashar Khan 2017. <[email protected]>
* This file is part of Google Developer Group's Android Application.
* Google Developer Group 's Android Application is free software : you can redistribute it and/or modify
* it under the terms of GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This Application is distributed in the hope that it will be useful
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this Source File.
* If not, see <http:www.gnu.org/licenses/>.
*/
package com.softminds.gdg.activities
import android.content.Intent
import android.graphics.Typeface
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.softminds.gdg.BuildConfig
import com.softminds.gdg.R
import com.softminds.gdg.utils.Constants
import kotlinx.android.synthetic.main.activity_about_us.*
class AboutUs : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_about_us)
val product = Typeface.createFromAsset(assets, Constants.PathConstants.productSansFontPath)
gdg_title.typeface = product
textView3.typeface = product
textView4.typeface = product
val versionText = getString(R.string.version) + " " + BuildConfig.VERSION_NAME
textView3.text = versionText
}
fun showSource(@Suppress("UNUSED_PARAMETER") view: View) {
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.github.com/coder3101/gdgApp")
startActivity(intent)
}
}
| gpl-3.0 | 2ebcccb8858d3cd943c45a65b6e7c6e8 | 36.865385 | 106 | 0.741493 | 4.034836 | false | false | false | false |
kenrube/Fantlab-client | app/src/main/kotlin/ru/fantlab/android/ui/modules/classificator/story/ClassificationStoryFragment.kt | 2 | 4416 | package ru.fantlab.android.ui.modules.classificator.story
import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.micro_grid_refresh_list.*
import kotlinx.android.synthetic.main.state_layout.*
import ru.fantlab.android.R
import ru.fantlab.android.data.dao.model.Classificator
import ru.fantlab.android.data.dao.model.ClassificatorModel
import ru.fantlab.android.helper.BundleConstant
import ru.fantlab.android.helper.Bundler
import ru.fantlab.android.ui.adapter.viewholder.ClassificatorViewHolder
import ru.fantlab.android.ui.base.BaseFragment
import ru.fantlab.android.ui.modules.classificator.ClassificatorPagerMvp
import ru.fantlab.android.ui.widgets.treeview.TreeNode
import ru.fantlab.android.ui.widgets.treeview.TreeViewAdapter
import java.util.*
class ClassificationStoryFragment : BaseFragment<ClassificationStoryMvp.View, ClassificationStoryPresenter>(),
ClassificationStoryMvp.View {
private var pagerCallback: ClassificatorPagerMvp.View? = null
var selectedItems = 0
override fun fragmentLayout() = R.layout.micro_grid_refresh_list
override fun providePresenter() = ClassificationStoryPresenter()
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
presenter.onFragmentCreated(arguments!!)
}
override fun onInitViews(classificators: ArrayList<ClassificatorModel>) {
hideProgress()
fastScroller.attachRecyclerView(recycler)
refresh.setOnRefreshListener(this)
val nodes = arrayListOf<TreeNode<*>>()
classificators.forEach {
val root = TreeNode(Classificator(it.name, it.descr, it.id))
nodes.add(root)
work(it, root, false)
}
val adapter = TreeViewAdapter(nodes, Arrays.asList(ClassificatorViewHolder()))
recycler.adapter = adapter
adapter.setOnTreeNodeListener(object : TreeViewAdapter.OnTreeNodeListener {
override fun onSelected(extra: Int, add: Boolean) {
if (add)
selectedItems++
else
selectedItems--
onSetTabCount(selectedItems)
pagerCallback?.onSelected(extra, add)
}
override fun onClick(node: TreeNode<*>, holder: RecyclerView.ViewHolder): Boolean {
val viewHolder = holder as ClassificatorViewHolder.ViewHolder
val state = viewHolder.checkbox.isChecked
if (!node.isLeaf) {
onToggle(!node.isExpand, holder)
if (!state && !node.isExpand) {
viewHolder.checkbox.isChecked = true
}
} else {
viewHolder.checkbox.isChecked = !state
}
return false
}
override fun onToggle(isExpand: Boolean, holder: RecyclerView.ViewHolder) {
val viewHolder = holder as ClassificatorViewHolder.ViewHolder
val ivArrow = viewHolder.arrow
val rotateDegree = if (isExpand) 90.0f else -90.0f
ivArrow.animate().rotationBy(rotateDegree)
.start()
}
})
}
private fun work(it: ClassificatorModel, root: TreeNode<Classificator>, lastLevel: Boolean) {
if (it.childs != null) {
val counter = root.childList.size - 1
it.childs.forEach {
val child = TreeNode(Classificator(it.name, it.descr, it.id))
if (lastLevel) {
val childB = TreeNode(Classificator(it.name, it.descr, it.id))
root.childList[counter].addChild(childB)
} else root.addChild(child)
work(it, root, true)
}
}
}
override fun showProgress(@StringRes resId: Int, cancelable: Boolean) {
refresh.isRefreshing = true
stateLayout.showProgress()
}
override fun hideProgress() {
refresh.isRefreshing = false
stateLayout.hideProgress()
}
override fun showErrorMessage(msgRes: String?) {
hideProgress()
super.showErrorMessage(msgRes)
}
override fun showMessage(titleRes: Int, msgRes: Int) {
hideProgress()
super.showMessage(titleRes, msgRes)
}
override fun onClick(v: View?) {
onRefresh()
}
override fun onRefresh() {
hideProgress()
}
fun onSetTabCount(count: Int) {
pagerCallback?.onSetBadge(4, count)
}
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is ClassificatorPagerMvp.View) {
pagerCallback = context
}
}
override fun onDetach() {
pagerCallback = null
super.onDetach()
}
companion object {
fun newInstance(workId: Int): ClassificationStoryFragment {
val view = ClassificationStoryFragment()
view.arguments = Bundler.start().put(BundleConstant.EXTRA, workId).end()
return view
}
}
} | gpl-3.0 | d6323ab1df37460d10c2cf979d9a591f | 27.681818 | 110 | 0.749094 | 3.863517 | false | false | false | false |
wordpress-mobile/AztecEditor-Android | aztec/src/main/kotlin/org/wordpress/aztec/watchers/event/sequence/ObservationQueue.kt | 1 | 3556 | package org.wordpress.aztec.watchers.event.sequence
import android.os.Build
import org.wordpress.aztec.watchers.event.IEventInjector
import org.wordpress.aztec.watchers.event.buckets.API25Bucket
import org.wordpress.aztec.watchers.event.buckets.API26Bucket
import org.wordpress.aztec.watchers.event.buckets.Bucket
import org.wordpress.aztec.watchers.event.text.TextWatcherEvent
class ObservationQueue(val injector: IEventInjector) : EventSequence<TextWatcherEvent>() {
val buckets = ArrayList<Bucket>()
init {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
buckets.add(API26Bucket())
} else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
buckets.add(API25Bucket())
}
/*
remember to add here any other buckets and init logic as suitable, depending on the context
*/
}
fun hasActiveBuckets() : Boolean {
return buckets.size > 0
}
override fun add(element: TextWatcherEvent): Boolean {
synchronized(this@ObservationQueue) {
val added: Boolean = super.add(element)
if (buckets.size == 0) {
return added
}
if (added) {
processQueue()
}
return added
}
}
private fun processQueue() {
// here let's check whether our current queue matches / fits any of the installed buckets
var foundOnePartialMatch = false
// if we only have 2 events and the first one is older than xxx milliseconds,
// that means that event is certainly not worth observing so let's discard that one
// we never pile up events we are not interested in this way
if (size == 2) {
val timeDistance = this.get(1).timestamp - this.get(0).timestamp
if (timeDistance > ObservationQueue.MAXIMUM_TIME_BETWEEN_EVENTS_IN_PATTERN_MS) {
removeAt(0)
}
}
// now let's continue processing
for (bucket in buckets) {
for (operation in bucket.userOperations) {
if (size < operation.sequence.size) {
if (operation.isUserOperationPartiallyObservedInSequence(this)) {
foundOnePartialMatch = true
}
} else {
// does this particular event look like a part of any of the user operations as defined in this bucket?
val result = operation.isUserOperationObservedInSequence(this)
if (operation.isFound(result)) {
// replace user operation with ONE TextWatcherEvent and inject this one in the actual
// textwatchers
val replacementEvent = operation.buildReplacementEventWithSequenceData(this)
injector.executeEvent(replacementEvent)
clear()
}
// regardless of the operation being found, let's check if it needs the queue to be cleared
if (operation.needsClear(result)) {
clear()
}
}
}
}
// we didn't find neither a partial match nor a total match, let's just clear the queue
if (size > 0 && !foundOnePartialMatch) {
// immediately discard the queue
clear()
}
}
companion object {
val MAXIMUM_TIME_BETWEEN_EVENTS_IN_PATTERN_MS = 100
}
}
| mpl-2.0 | f96bf090b1e3d8e3895bb8edf2da3a25 | 37.652174 | 123 | 0.585489 | 4.898072 | false | false | false | false |
ThomasVadeSmileLee/cyls | src/main/kotlin/com/scienjus/smartqq/model/GroupMessage.kt | 1 | 1244 | package com.scienjus.smartqq.model
import com.github.salomonbrys.kotson.getObject
import com.github.salomonbrys.kotson.string
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
/**
* 群消息.
* @author ScienJus
* *
* @author [Liang Ding](http://88250.b3log.org)
* *
* @date 15/12/19.
*/
data class GroupMessage(
var groupId: Long = 0L,
var time: Long = 0L,
var content: String? = "",
var userId: Long = 0L,
var font: Font? = null
) {
constructor(json: JsonObject) : this() {
val cont = json.getAsJsonArray("content")
this.font = cont.get(0).asJsonArray.getObject(1)
val size = cont.size()
val contentBuilder = StringBuilder()
(1 until size).forEach { i ->
contentBuilder.append(cont.get(i).run {
if (this@run is JsonPrimitive && [email protected]) {
[email protected]
} else {
[email protected]()
}
})
}
this.content = contentBuilder.toString()
this.time = json.get("time").asLong
this.groupId = json.get("group_code").asLong
this.userId = json.get("send_uin").asLong
}
}
| mit | ee47a24b9632ccff62ec72e6382a4e5b | 26.511111 | 69 | 0.572698 | 3.740181 | false | false | false | false |
mwolfson/android-historian | app/src/main/java/com/designdemo/uaha/net/WikiApiFactory.kt | 1 | 877 | package com.designdemo.uaha.net
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
class WikiApiFactory {
@JvmOverloads
fun create(okHttpClient: OkHttpClient? = null): WikiApiService = run {
val interceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
val client = OkHttpClient().newBuilder()
.addInterceptor(interceptor)
.build()
val retrofit = Retrofit.Builder()
.baseUrl(baseUrlWiki)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build()
retrofit.create(WikiApiService::class.java)
}
companion object {
const val baseUrlWiki = "https://en.wikipedia.org/"
}
}
| apache-2.0 | b3daec1c1deeda509266c5b7e47f8d28 | 27.290323 | 94 | 0.663626 | 5.220238 | false | false | false | false |
Instagram/ig-json-parser | demo/igmodel/src/main/java/com/instagram/common/json/app/igmodel/playground/KotlinPlaygroundData.kt | 1 | 663 | /*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.instagram.common.json.app.igmodel.playground
import com.instagram.common.json.annotation.JsonField
import com.instagram.common.json.annotation.JsonType
@JsonType
data class KotlinIsFun(
@JsonField(fieldName = "data") var data: String = "",
@JsonField(fieldName = "fun") var isFun: Nested = Nested()
)
@JsonType
data class Nested(
@JsonField(fieldName = "foo") var foo: String? = null,
@JsonField(fieldName = "bar") var bar: String? = null
)
| mit | 88534e3fc227ef5b90fa148e2414260c | 27.826087 | 66 | 0.722474 | 3.683333 | false | false | false | false |
westnordost/StreetComplete | app/src/main/java/de/westnordost/streetcomplete/data/osm/mapdata/RelationTable.kt | 1 | 520 | package de.westnordost.streetcomplete.data.osm.mapdata
object RelationTable {
const val NAME = "osm_relations"
object Columns {
const val ID = "id"
const val VERSION = "version"
const val TAGS = "tags"
const val MEMBERS = "members"
}
const val CREATE = """
CREATE TABLE $NAME (
${Columns.ID} int PRIMARY KEY,
${Columns.VERSION} int NOT NULL,
${Columns.TAGS} blob,
${Columns.MEMBERS} blob NOT NULL
);"""
}
| gpl-3.0 | 02d4a7b2a262bb9019045ca78d5fb1f2 | 25 | 54 | 0.559615 | 4.227642 | false | false | false | false |
StepicOrg/stepic-android | app/src/main/java/org/stepik/android/view/streak/ui/dialog/StreakNotificationDialogFragment.kt | 2 | 2327 | package org.stepik.android.view.streak.ui.dialog
import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.android.synthetic.main.header_streak_suggestion_dialog.view.*
import org.stepic.droid.R
import org.stepic.droid.analytic.Analytic
import org.stepic.droid.base.App
import org.stepic.droid.ui.dialogs.TimeIntervalPickerDialogFragment
import ru.nobird.android.view.base.ui.extension.argument
import ru.nobird.android.view.base.ui.extension.showIfNotExists
import javax.inject.Inject
class StreakNotificationDialogFragment : DialogFragment() {
companion object {
const val TAG = "StreakSuggestionDialogFragment"
fun newInstance(title: String, message: String, positiveEvent: String): DialogFragment =
StreakNotificationDialogFragment()
.apply {
this.title = title
this.message = message
this.positiveEvent = positiveEvent
}
}
@Inject
lateinit var analytic: Analytic
private var title: String by argument()
private var message: String by argument()
private var positiveEvent: String by argument()
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
App.component().inject(this)
val titleView =
View.inflate(context, R.layout.header_streak_suggestion_dialog, null)
.apply {
headerTitle.text = title
}
return MaterialAlertDialogBuilder(requireContext())
.setCustomTitle(titleView)
.setMessage(message)
.setPositiveButton(R.string.ok) { _, _ ->
analytic.reportEvent(positiveEvent)
TimeIntervalPickerDialogFragment
.newInstance()
.showIfNotExists(requireFragmentManager(), TimeIntervalPickerDialogFragment.TAG)
}
.setNegativeButton(R.string.later_tatle) { _, _ ->
(activity as? Callback)
?.onStreakNotificationDialogCancelled()
}
.create()
}
interface Callback {
fun onStreakNotificationDialogCancelled()
}
} | apache-2.0 | 11efdef8ad9b7d4ecdb6ae5f28775026 | 34.815385 | 100 | 0.658788 | 5.114286 | false | false | false | false |
StepicOrg/stepic-android | app/src/main/java/org/stepik/android/view/step/ui/delegate/StepNavigationDelegate.kt | 2 | 2017 | package org.stepik.android.view.step.ui.delegate
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import kotlinx.android.synthetic.main.view_step_navigation.view.*
import org.stepic.droid.R
import org.stepik.android.domain.step.model.StepNavigationDirection
class StepNavigationDelegate(
private val containerView: View,
private val onDirectionClicked: (StepNavigationDirection) -> Unit
) {
private val nextButton = containerView.stepNavigationNext
private val prevButton = containerView.stepNavigationPrev
init {
containerView.isVisible = false
prevButton.setOnClickListener { onDirectionClicked(StepNavigationDirection.PREV) }
nextButton.setOnClickListener { onDirectionClicked(StepNavigationDirection.NEXT) }
}
fun setState(directions: Set<StepNavigationDirection>) {
containerView.isVisible = directions.isNotEmpty()
val isPrevAvailable = StepNavigationDirection.PREV in directions
val isNextAvailable = StepNavigationDirection.NEXT in directions
prevButton.isVisible = isPrevAvailable
nextButton.isVisible = isNextAvailable
when {
!isPrevAvailable && isNextAvailable -> {
nextButton.gravity = Gravity.CENTER
}
isPrevAvailable && !isNextAvailable -> {
prevButton.setText(R.string.step_navigation_prev)
prevButton.layoutParams = prevButton.layoutParams.apply { width = 0 }
prevButton.compoundDrawablePadding = nextButton.compoundDrawablePadding
}
isPrevAvailable && isNextAvailable -> {
prevButton.text = null
prevButton.layoutParams = prevButton.layoutParams.apply { width = ViewGroup.LayoutParams.WRAP_CONTENT }
prevButton.compoundDrawablePadding = 0
nextButton.gravity = Gravity.CENTER_VERTICAL or Gravity.START
}
}
}
} | apache-2.0 | 454e85c0e177ef1959853326d5fa1a82 | 37.075472 | 119 | 0.701537 | 5.280105 | false | false | false | false |
StepicOrg/stepic-android | model/src/main/java/org/stepik/android/model/Block.kt | 2 | 1866 | package org.stepik.android.model
import android.os.Parcel
import android.os.Parcelable
import com.google.gson.JsonElement
import com.google.gson.annotations.SerializedName
import org.stepik.android.model.code.CodeOptions
//more fields look at stepik.org/api/steps/14671
data class Block(
@SerializedName("name")
val name: String? = null,
@SerializedName("text")
val text: String? = null,
@SerializedName("video")
var video: Video? = null, //always external video
@SerializedName("options")
val options: CodeOptions? = null,
/**
* ignored fields only for step source
*/
@SerializedName("subtitle_files")
val subtitleFiles: JsonElement? = null,
@SerializedName("subtitles")
val subtitles: JsonElement? = null,
@SerializedName("source")
val source: JsonElement? = null,
@SerializedName("tests_archive")
val testsArchive: JsonElement? = null,
@SerializedName("feedback_correct")
val feedbackCorrect: JsonElement? = null,
@SerializedName("feedback_wrong")
val feedbackWrong: JsonElement? = null
) : Parcelable {
override fun describeContents(): Int = 0
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(this.name)
dest.writeString(this.text)
dest.writeParcelable(this.video, flags)
dest.writeParcelable(this.options, flags)
}
companion object CREATOR: Parcelable.Creator<Block> {
override fun createFromParcel(parcel: Parcel): Block =
Block(
parcel.readString(),
parcel.readString(),
parcel.readParcelable(Video::class.java.classLoader),
parcel.readParcelable(CodeOptions::class.java.classLoader)
)
override fun newArray(size: Int): Array<Block?> =
arrayOfNulls(size)
}
}
| apache-2.0 | 5ab1780810287a823b24cf4f267eb871 | 27.272727 | 74 | 0.665059 | 4.47482 | false | true | false | false |
enihsyou/Sorting-algorithm | CodeForces/908G.kt | 1 | 647 | import java.util.*
fun main(args: Array<String>) {
fun S(i: Int): Int
= i.toString().toList().sorted().joinToString(separator = "").toInt()
//
// for (e in 0..100 step 10) {
val nL = mutableListOf<Int>()
// for (i in e until 10 + e) {
// val su = S(i)
// nL += su
// println("$i -> $su, sum: ${nL.sum()}")
// }
// println()
// }
val s = Scanner(System.`in`)
var su = 0
with(s) {
val lim = s.nextInt()
for (i in 1..lim) {
val su = S(i)
// println("$i -> $su, sum: ${nL.sum()}")
}
}
println(nL.sum())
}
| mit | 11dc2479646d6cec5f910b13c3d9f747 | 22.107143 | 77 | 0.421947 | 3.140777 | false | false | false | false |
Heiner1/AndroidAPS | danar/src/main/java/info/nightscout/androidaps/danar/comm/MsgStatus.kt | 1 | 1505 | package info.nightscout.androidaps.danar.comm
import dagger.android.HasAndroidInjector
import info.nightscout.shared.logging.LTag
class MsgStatus(
injector: HasAndroidInjector
) : MessageBase(injector) {
init {
setCommand(0x020B)
aapsLogger.debug(LTag.PUMPCOMM, "New message")
}
override fun handleMessage(bytes: ByteArray) {
danaPump.dailyTotalUnits = intFromBuff(bytes, 0, 3) / 750.0
val isExtendedInProgress = intFromBuff(bytes, 3, 1) == 1
val extendedBolusMinutes = intFromBuff(bytes, 4, 2)
val extendedBolusAmount = intFromBuff(bytes, 6, 2) / 100.0
val lastBolusAmount = intFromBuff(bytes, 13, 2) / 100.0
if (lastBolusAmount != 0.0) {
danaPump.lastBolusTime = dateTimeFromBuff(bytes, 8)
danaPump.lastBolusAmount = lastBolusAmount
}
danaPump.iob = intFromBuff(bytes, 15, 2) / 100.0
aapsLogger.debug(LTag.PUMPCOMM, "Daily total: " + danaPump.dailyTotalUnits)
aapsLogger.debug(LTag.PUMPCOMM, "Is extended bolus running: $isExtendedInProgress")
aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus min: $extendedBolusMinutes")
aapsLogger.debug(LTag.PUMPCOMM, "Extended bolus amount: $extendedBolusAmount")
aapsLogger.debug(LTag.PUMPCOMM, "Last bolus time: " + danaPump.lastBolusTime)
aapsLogger.debug(LTag.PUMPCOMM, "Last bolus amount: " + danaPump.lastBolusAmount)
aapsLogger.debug(LTag.PUMPCOMM, "IOB: " + danaPump.iob)
}
} | agpl-3.0 | 9f7594fec1afbcc3589b86550276ebbf | 43.294118 | 91 | 0.690365 | 4.215686 | false | false | false | false |
k9mail/k-9 | mail/common/src/main/java/com/fsck/k9/mail/internet/MimeHeader.kt | 2 | 4289 | package com.fsck.k9.mail.internet
import com.fsck.k9.mail.Header
import com.fsck.k9.mail.internet.MimeHeader.Field.NameValueField
import com.fsck.k9.mail.internet.MimeHeader.Field.RawField
import java.io.IOException
import java.io.OutputStream
import java.util.ArrayList
import java.util.LinkedHashSet
class MimeHeader {
private val fields: MutableList<Field> = ArrayList()
val headerNames: Set<String>
get() = fields.mapTo(LinkedHashSet()) { it.name }
val headers: List<Header>
get() = fields.map { Header(it.name, it.value) }
var checkHeaders = false
fun clear() {
fields.clear()
}
fun getFirstHeader(name: String): String? {
return getHeader(name).firstOrNull()
}
fun addHeader(name: String, value: String) {
requireValidHeader(name, value)
val field = NameValueField(name, value)
fields.add(field)
}
fun addRawHeader(name: String, raw: String) {
requireValidRawHeader(name, raw)
val field = RawField(name, raw)
fields.add(field)
}
fun setHeader(name: String, value: String) {
removeHeader(name)
addHeader(name, value)
}
fun getHeader(name: String): Array<String> {
return fields.asSequence()
.filter { field -> field.name.equals(name, ignoreCase = true) }
.map { field -> field.value }
.toList()
.toTypedArray()
}
fun removeHeader(name: String) {
fields.removeAll { field -> field.name.equals(name, ignoreCase = true) }
}
override fun toString(): String {
return buildString {
appendFields()
}
}
@Throws(IOException::class)
fun writeTo(out: OutputStream) {
val writer = out.writer().buffered(1024)
writer.appendFields()
writer.flush()
}
private fun Appendable.appendFields() {
for (field in fields) {
when (field) {
is RawField -> append(field.raw)
is NameValueField -> appendNameValueField(field)
}
append(CRLF)
}
}
private fun Appendable.appendNameValueField(field: Field) {
append(field.name)
append(": ")
append(field.value)
}
private fun requireValidHeader(name: String, value: String) {
if (checkHeaders) {
checkHeader(name, value)
}
}
private fun requireValidRawHeader(name: String, raw: String) {
if (checkHeaders) {
if (!raw.startsWith(name)) throw AssertionError("Raw header value needs to start with header name")
val delimiterIndex = raw.indexOf(':')
val value = if (delimiterIndex == raw.lastIndex) "" else raw.substring(delimiterIndex + 1).trimStart()
checkHeader(name, value)
}
}
private fun checkHeader(name: String, value: String) {
try {
MimeHeaderChecker.checkHeader(name, value)
} catch (e: MimeHeaderParserException) {
// Use AssertionError so we crash the app
throw AssertionError("Invalid header", e)
}
}
companion object {
const val SUBJECT = "Subject"
const val HEADER_CONTENT_TYPE = "Content-Type"
const val HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding"
const val HEADER_CONTENT_DISPOSITION = "Content-Disposition"
const val HEADER_CONTENT_ID = "Content-ID"
}
private sealed class Field(val name: String) {
abstract val value: String
class NameValueField(name: String, override val value: String) : Field(name) {
override fun toString(): String {
return "$name: $value"
}
}
class RawField(name: String, val raw: String) : Field(name) {
override val value: String
get() {
val delimiterIndex = raw.indexOf(':')
return if (delimiterIndex == raw.lastIndex) {
""
} else {
raw.substring(delimiterIndex + 1).trim()
}
}
override fun toString(): String {
return raw
}
}
}
}
| apache-2.0 | b1c57067c29ef077922c9fa5626607dd | 28.376712 | 114 | 0.57869 | 4.430785 | false | false | false | false |
Tandrial/Advent_of_Code | src/aoc2018/kot/Day12.kt | 1 | 1787 | package aoc2018.kot
import java.io.File
object Day12 {
fun partOne(input: List<String>): Int {
val (state, rules) = parse(input)
return (0 until 20).fold(state) { s, _ -> nextGen(s, rules) }.sum()
}
fun partTwo(input: List<String>): Long {
val (state, rules) = parse(input)
var currentSum = state.sum()
var next = state
val lastDifferences = mutableListOf<Int>()
var cnt = 0
while (lastDifferences.size < 2 || lastDifferences.any { it != lastDifferences.last() }) {
next = nextGen(next, rules)
lastDifferences.add(next.sum() - currentSum)
if (lastDifferences.size > 100) lastDifferences.removeAt(0)
currentSum = next.sum()
cnt++
}
return (50000000000 - cnt) * lastDifferences.last() + currentSum
}
private fun nextGen(state: Set<Int>, rules: List<String>): Set<Int> {
val left = state.min()!!
val right = state.max()!!
val next = mutableSetOf<Int>()
for (i in (left - 2..right + 2)) {
val pattern = (-2..2).joinToString(separator = "") {
when (i + it) {
in state -> "#"
else -> "."
}
}
if (rules.any { it == pattern }) next.add(i)
}
return next
}
private fun parse(input: List<String>): Pair<Set<Int>, List<String>> {
val state = input[0].split(": ")[1]
val rules = input.drop(2).mapNotNull { line ->
val (lhs, rhs) = line.split(" => ")
when (rhs) {
"#" -> lhs
else -> null
}
}
return Pair(state.withIndex().filter { it.value == '#' }.map { it.index }.toSet(), rules)
}
}
fun main(args: Array<String>) {
val input = File("./input/2018/Day12_input.txt").readLines()
println("Part One = ${Day12.partOne(input)}")
println("Part Two = ${Day12.partTwo(input)}")
}
| mit | cc3d37114cf9c895387ff844e59aeff8 | 27.822581 | 94 | 0.574147 | 3.449807 | false | false | false | false |
KotlinNLP/SimpleDNN | src/test/kotlin/core/layers/merge/affine/AffineLayerParametersSpec.kt | 1 | 2140 | /* Copyright 2016-present The KotlinNLP Authors. All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain contexte at http://mozilla.org/MPL/2.0/.
* ------------------------------------------------------------------*/
package core.layers.merge.affine
import com.kotlinnlp.simplednn.core.functionalities.initializers.ConstantInitializer
import com.kotlinnlp.simplednn.core.functionalities.initializers.RandomInitializer
import com.kotlinnlp.simplednn.core.functionalities.randomgenerators.RandomGenerator
import com.kotlinnlp.simplednn.core.layers.models.merge.affine.AffineLayerParameters
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.whenever
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import kotlin.test.assertEquals
/**
*
*/
class AffineLayerParametersSpec : Spek({
describe("a AffineLayerParametersS") {
context("initialization") {
context("dense input") {
var k = 0
val initValues = doubleArrayOf(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0)
val randomGenerator = mock<RandomGenerator>()
whenever(randomGenerator.next()).thenAnswer { initValues[k++] }
val params = AffineLayerParameters(
inputsSize = listOf(2, 3),
outputSize = 2,
weightsInitializer = RandomInitializer(randomGenerator),
biasesInitializer = ConstantInitializer(0.9))
val w1 = params.w[0].values
val w2 = params.w[1].values
val b = params.b.values
it("should contain the expected initialized w1") {
(0 until w1.length).forEach { i -> assertEquals(initValues[i], w1[i]) }
}
it("should contain the expected initialized w2") {
(0 until w2.length).forEach { i -> assertEquals(initValues[4 + i], w2[i]) }
}
it("should contain the expected initialized biases") {
(0 until b.length).forEach { i -> assertEquals(0.9, b[i]) }
}
}
}
}
})
| mpl-2.0 | 6eaac68893993fcaa0b3382e43e3e295 | 34.666667 | 88 | 0.662617 | 4.060721 | false | false | false | false |
InsideZhou/Instep | dao/src/main/kotlin/instep/dao/sql/impl/DefaultSQLPlanExecutor.kt | 1 | 7117 | package instep.dao.sql.impl
import instep.Instep
import instep.dao.sql.*
import instep.typeconversion.TypeConversion
import java.sql.Connection
import java.sql.ResultSet
import java.sql.SQLException
import java.time.*
import java.time.temporal.Temporal
@Suppress("MemberVisibilityCanBePrivate", "UNCHECKED_CAST")
open class DefaultSQLPlanExecutor<S : SQLPlan<*>>(
val connectionProvider: ConnectionProvider,
val preparedStatementGenerator: PreparedStatementGenerator,
val typeconvert: TypeConversion,
) : SQLPlanExecutor<S> {
constructor(connectionProvider: ConnectionProvider) : this(
connectionProvider,
Instep.make(PreparedStatementGenerator::class.java),
Instep.make(TypeConversion::class.java),
)
constructor() : this(Instep.make(ConnectionProvider::class.java))
override fun execute(plan: S) {
val conn = connectionProvider.getConnection()
try {
val stmt = preparedStatementGenerator.generate(conn, connectionProvider.dialect, plan)
stmt.execute()
if (plan.subPlans.isNotEmpty()) {
plan.subPlans.forEach {
execute(it as S)
}
}
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
}
override fun executeString(plan: S): String {
val conn = connectionProvider.getConnection()
try {
val rs = executeResultSet(conn, plan)
if (!rs.next() || rs.wasNull()) return ""
return rs.getString(1)
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
}
override fun executeLong(plan: S): Long {
val conn = connectionProvider.getConnection()
try {
val rs = executeResultSet(conn, plan)
if (!rs.next() || rs.wasNull()) return 0L
return rs.getLong(1)
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
}
override fun executeDouble(plan: S): Double {
val conn = connectionProvider.getConnection()
try {
val rs = executeResultSet(conn, plan)
if (!rs.next() || rs.wasNull()) return 0.0
return rs.getDouble(1)
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
}
override fun <R : Temporal> executeTemporal(plan: S, cls: Class<R>): R? {
val conn = connectionProvider.getConnection()
try {
val resultSet = executeResultSet(conn, plan)
if (!resultSet.next() || resultSet.wasNull()) return null
val resultSetDelegate = Instep.make(ResultSetDelegate::class.java)
val rs = resultSetDelegate.getDelegate(connectionProvider.dialect, resultSet)
val colIndex = 1
return when (cls) {
Instant::class.java -> rs.getInstant(colIndex) as R
LocalDate::class.java -> rs.getLocalDate(colIndex) as R
LocalTime::class.java -> rs.getLocalTime(colIndex) as R
LocalDateTime::class.java -> rs.getLocalDateTime(colIndex) as R
OffsetDateTime::class.java -> rs.getOffsetDateTime(colIndex) as R
else -> rs.getObject(colIndex, cls)
}
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
}
override fun executeUpdate(plan: S): Int {
val conn = connectionProvider.getConnection()
try {
return preparedStatementGenerator.generate(conn, connectionProvider.dialect, plan).executeUpdate()
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
}
override fun executeResultSet(conn: Connection, plan: S): ResultSet {
val stmt = preparedStatementGenerator.generate(conn, connectionProvider.dialect, plan)
return when (plan) {
is TableInsertPlan -> {
stmt.executeUpdate()
stmt.generatedKeys
}
else -> stmt.executeQuery()
}
}
override fun <T : Any> execute(plan: S, cls: Class<T>): List<T> {
val conn = connectionProvider.getConnection()
val tableRows = mutableListOf<TableRow>()
val dataRows = mutableListOf<DataRow>()
try {
val rs = executeResultSet(conn, plan)
when {
plan is TableSelectPlan && plan.join.isEmpty() && !DataRow::class.java.isAssignableFrom(cls) -> {
while (rs.next()) {
tableRows.add(TableRow.createInstance(plan.from, rs))
}
}
plan is TableInsertPlan && !DataRow::class.java.isAssignableFrom(cls) -> {
while (rs.next()) {
tableRows.add(TableRow.createInstance(plan.table, rs))
}
}
else -> {
typeconvert.getConverter(ResultSet::class.java, DataRow::class.java)!!.let { converter ->
while (rs.next()) {
dataRows.add(converter.convert(rs))
}
}
}
}
}
catch (e: SQLException) {
throw SQLPlanExecutionException(e)
}
finally {
connectionProvider.releaseConnection(conn)
}
if (tableRows.size > 0) return tableRowsToObjects(tableRows, cls)
if (dataRows.size > 0) return dataRowsToObjects(dataRows, cls)
return emptyList()
}
protected open fun <T : Any> tableRowsToObjects(rows: List<TableRow>, cls: Class<T>): List<T> {
if (cls.isAssignableFrom(TableRow::class.java)) {
return rows.map { it as T }
}
typeconvert.getConverter(TableRow::class.java, cls)?.let { converter ->
return rows.map { converter.convert(it) }
}
return rows.map { row -> row.fillUp(cls) }
}
protected open fun <T : Any> dataRowsToObjects(rows: List<DataRow>, cls: Class<T>): List<T> {
if (cls.isAssignableFrom(DataRow::class.java)) {
return rows.map { it as T }
}
typeconvert.getConverter(DataRow::class.java, cls)?.let { converter ->
return rows.map { converter.convert(it) }
}
return rows.map { row -> row.fillUp(cls) }
}
}
data class ResultSetColumnInfo(val index: Int, val label: String, val type: Int, val typeName: String) | bsd-2-clause | a0780668a55aead9cd6e2c2f42724b4a | 31.801843 | 113 | 0.573978 | 4.773307 | false | false | false | false |
byu-oit/android-byu-suite-v2 | support/src/main/java/edu/byu/support/fragment/ByuFragment.kt | 2 | 2462 | package edu.byu.support.fragment
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.support.v4.app.Fragment
import java.util.HashSet
import edu.byu.support.activity.ByuActivity
import edu.byu.support.retrofit.ByuCallManager
import edu.byu.support.retrofit.ByuCallback
import retrofit2.Call
/**
* Created by benjamin on 5/12/16
*/
open class ByuFragment: Fragment(), ByuCallManager {
companion object {
private const val TITLE = "title"
inline fun <reified T: ByuFragment> newInstance(title: String?, bundle: Bundle = Bundle()): T {
val fragment = T::class.java.newInstance()
bundle.putAll(getArgsBundleWithTitle(title))
fragment.arguments = bundle
return fragment
}
fun getArgsBundleWithTitle(title: String?): Bundle {
val bundle = Bundle()
if (title != null) {
bundle.putString(TITLE, title)
}
return bundle
}
}
private val calls = HashSet<Call<*>>(ByuCallManager.INITIAL_CAPACITY)
private var activity: ByuActivity? = null
open fun getTitle(): String? = arguments?.getString(TITLE)
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is ByuActivity) {
activity = context
}
}
override fun <T> enqueueCall(call: Call<T>, callback: ByuCallback<T>) {
calls.add(call)
call.enqueue(callback)
}
override fun removeCall(call: Call<*>) {
calls.remove(call)
}
override fun cancelCall(call: Call<*>) {
if (calls.contains(call)) {
call.cancel()
calls.remove(call)
}
}
override fun cancelAllCalls() {
val iterator = calls.iterator()
while (iterator.hasNext()) {
val call = iterator.next()
call.cancel()
iterator.remove()
}
}
override fun areAllCallsCompleted(): Boolean {
return calls.isEmpty()
}
override fun onDestroy() {
super.onDestroy()
for (call in calls) {
call.cancel()
}
}
fun showProgressDialog() {
activity?.showProgressDialog()
}
fun showProgressDialog(cancelable: Boolean) {
activity?.showProgressDialog(cancelable)
}
fun dismissProgressDialog() {
activity?.dismissProgressDialog()
}
fun showErrorDialog(errorMessage: String) {
activity?.showErrorDialog(errorMessage)
}
fun showErrorDialog(errorMessage: String, onButtonPressedListener: DialogInterface.OnClickListener?) {
activity?.showErrorDialog(errorMessage, onButtonPressedListener)
}
fun startChromeTabView(url: String) {
activity?.startChromeTabView(url)
}
}
| apache-2.0 | d61994cc964f07b3f73b478df20c05c4 | 20.787611 | 103 | 0.726239 | 3.573295 | false | false | false | false |
efficios/jabberwocky | jabberwocky-core/src/main/kotlin/com/efficios/jabberwocky/views/xychart/model/provider/statesystem/StateSystemXYChartProvider.kt | 2 | 1852 | /*
* Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <[email protected]>
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package com.efficios.jabberwocky.views.xychart.model.provider.statesystem
import ca.polymtl.dorsal.libdelorean.IStateSystemReader
import com.efficios.jabberwocky.analysis.statesystem.StateSystemAnalysis
import com.efficios.jabberwocky.views.xychart.model.provider.XYChartModelProvider
import javafx.beans.property.ObjectProperty
import javafx.beans.property.ReadOnlyObjectProperty
import javafx.beans.property.SimpleObjectProperty
abstract class StateSystemXYChartProvider(providerName: String,
stateSystemAnalysis: StateSystemAnalysis) : XYChartModelProvider(providerName) {
private val stateSystemProperty: ObjectProperty<IStateSystemReader?> = SimpleObjectProperty(null)
fun stateSystemProperty(): ReadOnlyObjectProperty<IStateSystemReader?> = stateSystemProperty
var stateSystem
get() = stateSystemProperty.get()
private set(value) = stateSystemProperty.set(value)
init {
/*
* Change listener which will take care of keeping the target state
* system up to date.
*/
traceProjectProperty().addListener { _, _, newProject ->
stateSystem = if (newProject != null
&& stateSystemAnalysis.appliesTo(newProject)
&& stateSystemAnalysis.canExecute(newProject)) {
// TODO Cache this?
stateSystemAnalysis.execute(newProject, null, null)
} else {
null
}
}
}
}
| epl-1.0 | 711ed43798ac4677ef070f4e962b78e7 | 40.155556 | 122 | 0.701404 | 4.938667 | false | false | false | false |
GunoH/intellij-community | plugins/kotlin/base/scripting/src/org/jetbrains/kotlin/idea/core/script/configuration/loader/DefaultScriptConfigurationLoader.kt | 4 | 3665 | // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.core.script.configuration.loader
import com.intellij.openapi.diagnostic.ControlFlowException
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.idea.core.script.configuration.cache.CachedConfigurationInputs
import org.jetbrains.kotlin.idea.core.script.configuration.cache.ScriptConfigurationSnapshot
import org.jetbrains.kotlin.idea.core.script.logScriptingConfigurationErrors
import org.jetbrains.kotlin.idea.core.script.scriptingDebugLog
import org.jetbrains.kotlin.idea.core.script.settings.KotlinScriptingSettings
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinition
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinition
import org.jetbrains.kotlin.scripting.resolve.KtFileScriptSource
import org.jetbrains.kotlin.scripting.resolve.LegacyResolverWrapper
import org.jetbrains.kotlin.scripting.resolve.refineScriptCompilationConfiguration
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.asDiagnostics
import kotlin.script.experimental.api.valueOrNull
import kotlin.script.experimental.dependencies.AsyncDependenciesResolver
open class DefaultScriptConfigurationLoader(val project: Project) : ScriptConfigurationLoader {
override fun shouldRunInBackground(scriptDefinition: ScriptDefinition): Boolean =
scriptDefinition
.asLegacyOrNull<KotlinScriptDefinition>()
?.dependencyResolver
?.let { it is AsyncDependenciesResolver || it is LegacyResolverWrapper }
?: false
override fun loadDependencies(
isFirstLoad: Boolean,
ktFile: KtFile,
scriptDefinition: ScriptDefinition,
context: ScriptConfigurationLoadingContext
): Boolean {
if (project.isDisposed) return false
val virtualFile = ktFile.originalFile.virtualFile
val result = getConfigurationThroughScriptingApi(ktFile, virtualFile, scriptDefinition)
if (KotlinScriptingSettings.getInstance(project).autoReloadConfigurations(scriptDefinition)) {
context.saveNewConfiguration(virtualFile, result)
} else {
context.suggestNewConfiguration(virtualFile, result)
}
return true
}
protected fun getConfigurationThroughScriptingApi(
file: KtFile,
vFile: VirtualFile,
scriptDefinition: ScriptDefinition
): ScriptConfigurationSnapshot {
scriptingDebugLog(file) { "start dependencies loading" }
val inputs = getInputsStamp(vFile, file)
val scriptingApiResult = try {
refineScriptCompilationConfiguration(
KtFileScriptSource(file), scriptDefinition, file.project
)
} catch (e: Throwable) {
if (e is ControlFlowException) throw e
ResultWithDiagnostics.Failure(listOf(e.asDiagnostics()))
}
val result = ScriptConfigurationSnapshot(
inputs,
scriptingApiResult.reports,
scriptingApiResult.valueOrNull()
)
scriptingDebugLog(file) { "finish dependencies loading" }
logScriptingConfigurationErrors(vFile, result)
return result
}
protected open fun getInputsStamp(virtualFile: VirtualFile, file: KtFile): CachedConfigurationInputs {
return CachedConfigurationInputs.PsiModificationStamp.get(project, virtualFile, file)
}
}
| apache-2.0 | 450f1d42abb31747ad40f20206490b78 | 41.616279 | 158 | 0.754161 | 5.389706 | false | true | false | false |
GunoH/intellij-community | platform/statistics/src/com/intellij/internal/statistic/utils/DumpLaunchParametersStarter.kt | 6 | 3178 | // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.internal.statistic.utils
import com.google.gson.GsonBuilder
import com.intellij.idea.AppExitCodes
import com.intellij.openapi.application.ModernApplicationStarter
import java.lang.management.ManagementFactory
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.absolutePathString
import kotlin.io.path.writeText
import kotlin.system.exitProcess
/*
{
"cmdArguments": [
"/private/var/folders/jx/x1bytp2j53n6dj649pdpw7_m0000gr/T/1661441672/Contents/bin/xplat-launcher",
"--output",
"/private/var/folders/jx/x1bytp2j53n6dj649pdpw7_m0000gr/T/1661441672/Contents/bin/output.json"
],
"vmOptions": [
"-Didea.vendor.name\u003dJetBrains",
...
"--add-opens\u003djdk.jdi/com.sun.tools.jdi\u003dALL-UNNAMED"
],
"environmentVariables": {
"PATH": "/Users/haze/Library/Python/3.8/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/haze/.cargo/bin:/Users/haze/Library/Application Support/JetBrains/Toolbox/scripts:/opt/homebrew/opt/fzf/bin",
..
"DYLD_FALLBACK_LIBRARY_PATH": "/Users/haze/work/intellij/master/community/native/XPlatLauncher/target/debug/deps:/Users/haze/work/intellij/master/community/native/XPlatLauncher/target/debug:/Users/haze/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib:/Users/haze/.rustup/toolchains/stable-aarch64-apple-darwin/lib:/Users/haze/lib:/usr/local/lib:/usr/lib"
},
"systemProperties": {
"java.specification.version": "17",
...
"splash": "true"
}
}
*/
data class DumpedLaunchParameters(
val cmdArguments: List<String>,
val vmOptions: List<String>,
val environmentVariables: Map<String, String>,
val systemProperties: Map<String, String>
)
internal class DumpLaunchParametersStarter : ModernApplicationStarter() {
override val commandName: String
get() = "dump-launch-parameters"
override fun premain(args: List<String>) {
val outputIndex = args.indexOfFirst { it == "-o" || it == "--output" } + 1
if (outputIndex == 0) {
System.err.println("Usage: -o/--output /path/to/output/file")
System.err.println("Current args: ${args.joinToString(" ")}")
exitProcess(AppExitCodes.STARTUP_EXCEPTION)
}
val outputFile = Path.of(args[outputIndex])
Files.createDirectories(outputFile.parent)
val gson = GsonBuilder().setPrettyPrinting().create()
@Suppress("UNCHECKED_CAST")
val dump = DumpedLaunchParameters(
cmdArguments = args,
vmOptions = ManagementFactory.getRuntimeMXBean().inputArguments,
systemProperties = System.getProperties() as? Map<String, String>
?: error("Failed to cast System.getProperties() result to Map<String, String>"),
environmentVariables = System.getenv()
)
val dumpJsonText = gson.toJson(dump)
println(dumpJsonText)
outputFile.writeText(dumpJsonText, Charsets.UTF_8)
println("Dumped to ${outputFile.absolutePathString()}")
exitProcess(0)
}
override suspend fun start(args: List<String>) {
exitProcess(0)
}
} | apache-2.0 | 37f7f81c12fa35522d731254bbb394b7 | 36.845238 | 393 | 0.732222 | 3.511602 | false | false | false | false |
jk1/intellij-community | platform/projectModel-impl/src/com/intellij/configurationStore/scheme-impl.kt | 2 | 5514 | // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.configurationStore
import com.intellij.openapi.extensions.AbstractExtensionPointBean
import com.intellij.openapi.options.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.io.FileUtil
import com.intellij.project.isDirectoryBased
import com.intellij.util.SmartList
import com.intellij.util.io.sanitizeFileName
import com.intellij.util.isEmpty
import com.intellij.util.lang.CompoundRuntimeException
import com.intellij.util.xmlb.annotations.Attribute
import org.jdom.Element
import java.io.OutputStream
import java.security.MessageDigest
import java.util.concurrent.atomic.AtomicReference
import java.util.function.Function
interface SchemeNameToFileName {
fun schemeNameToFileName(name: String): String
}
val OLD_NAME_CONVERTER: SchemeNameToFileName = object : SchemeNameToFileName {
override fun schemeNameToFileName(name: String) = FileUtil.sanitizeFileName(name, true)
}
val CURRENT_NAME_CONVERTER: SchemeNameToFileName = object : SchemeNameToFileName {
override fun schemeNameToFileName(name: String) = FileUtil.sanitizeFileName(name, false)
}
val MODERN_NAME_CONVERTER: SchemeNameToFileName = object : SchemeNameToFileName {
override fun schemeNameToFileName(name: String) = sanitizeFileName(name)
}
interface SchemeDataHolder<in T> {
/**
* You should call updateDigest() after read on init.
*/
fun read(): Element
fun updateDigest(scheme: T)
fun updateDigest(data: Element?)
}
/**
* A scheme processor can implement this interface to provide a file extension different from default .xml.
* @see SchemeProcessor
*/
interface SchemeExtensionProvider {
/**
* @return The scheme file extension **with a leading dot**, for example ".ext".
*/
val schemeExtension: String
}
// applicable only for LazySchemeProcessor
interface SchemeContentChangedHandler<MUTABLE_SCHEME> {
fun schemeContentChanged(scheme: MUTABLE_SCHEME, name: String, dataHolder: SchemeDataHolder<MUTABLE_SCHEME>)
}
abstract class LazySchemeProcessor<SCHEME, MUTABLE_SCHEME : SCHEME>(private val nameAttribute: String = "name") : SchemeProcessor<SCHEME, MUTABLE_SCHEME>() {
open fun getSchemeKey(attributeProvider: Function<String, String?>, fileNameWithoutExtension: String): String? {
return attributeProvider.apply(nameAttribute)
}
abstract fun createScheme(dataHolder: SchemeDataHolder<MUTABLE_SCHEME>,
name: String,
attributeProvider: Function<String, String?>,
isBundled: Boolean = false): MUTABLE_SCHEME
override fun writeScheme(scheme: MUTABLE_SCHEME): Element? = (scheme as SerializableScheme).writeScheme()
open fun isSchemeFile(name: CharSequence): Boolean = true
open fun isSchemeDefault(scheme: MUTABLE_SCHEME, digest: ByteArray): Boolean = false
open fun isSchemeEqualToBundled(scheme: MUTABLE_SCHEME): Boolean = false
}
class DigestOutputStream(val digest: MessageDigest) : OutputStream() {
override fun write(b: Int) {
digest.update(b.toByte())
}
override fun write(b: ByteArray, off: Int, len: Int) {
digest.update(b, off, len)
}
override fun toString(): String = "[Digest Output Stream] $digest"
}
fun Element.digest(): ByteArray {
// sha-1 is enough, sha-256 is slower, see https://www.nayuki.io/page/native-hash-functions-for-java
val digest = MessageDigest.getInstance("SHA-1")
serializeElementToBinary(this, DigestOutputStream(digest))
return digest.digest()
}
abstract class SchemeWrapper<out T>(name: String) : ExternalizableSchemeAdapter(), SerializableScheme {
protected abstract val lazyScheme: Lazy<T>
val scheme: T
get() = lazyScheme.value
override fun getSchemeState(): SchemeState = if (lazyScheme.isInitialized()) SchemeState.POSSIBLY_CHANGED else SchemeState.UNCHANGED
init {
this.name = name
}
}
abstract class LazySchemeWrapper<T>(name: String, dataHolder: SchemeDataHolder<SchemeWrapper<T>>, protected val writer: (scheme: T) -> Element) : SchemeWrapper<T>(name) {
protected val dataHolder: AtomicReference<SchemeDataHolder<SchemeWrapper<T>>> = AtomicReference(dataHolder)
override final fun writeScheme(): Element {
val dataHolder = dataHolder.get()
@Suppress("IfThenToElvis")
return if (dataHolder == null) writer(scheme) else dataHolder.read()
}
}
class InitializedSchemeWrapper<out T : Scheme>(scheme: T, private val writer: (scheme: T) -> Element) : SchemeWrapper<T>(scheme.name) {
override val lazyScheme: Lazy<T> = lazyOf(scheme)
override fun writeScheme(): Element = writer(scheme)
}
fun unwrapState(element: Element, project: Project, iprAdapter: SchemeManagerIprProvider?, schemeManager: SchemeManager<*>): Element? {
val data = if (project.isDirectoryBased) element.getChild("settings") else element
iprAdapter?.let {
it.load(data)
schemeManager.reload()
}
return data
}
fun wrapState(element: Element, project: Project): Element {
if (element.isEmpty() || !project.isDirectoryBased) {
element.name = "state"
return element
}
val wrapper = Element("state")
wrapper.addContent(element)
return wrapper
}
class BundledSchemeEP : AbstractExtensionPointBean() {
@Attribute("path")
var path: String? = null
}
fun SchemeManager<*>.save() {
val errors = SmartList<Throwable>()
save(errors)
CompoundRuntimeException.throwIfNotEmpty(errors)
} | apache-2.0 | 8c62a475769b03c07f161f5a8cd2e171 | 34.352564 | 170 | 0.75263 | 4.490228 | false | false | false | false |
linkedin/LiTr | litr/src/main/java/com/linkedin/android/litr/frameextract/behaviors/MediaMetadataExtractBehavior.kt | 1 | 2514 | /*
* Copyright 2021 LinkedIn Corporation
* All Rights Reserved.
*
* Licensed under the BSD 2-Clause License (the "License"). See License in the project root for
* license information.
*/
package com.linkedin.android.litr.frameextract.behaviors
import android.content.Context
import android.media.MediaMetadataRetriever
import android.net.Uri
import com.linkedin.android.litr.ExperimentalFrameExtractorApi
import com.linkedin.android.litr.frameextract.FrameExtractMode
import com.linkedin.android.litr.frameextract.FrameExtractParameters
@ExperimentalFrameExtractorApi
class MediaMetadataExtractBehavior(private val context: Context) : FrameExtractBehavior {
private var retrieverToMediaUri: RetrieverToMediaUri? = null
@Synchronized
private fun setupRetriever(mediaUri: Uri): MediaMetadataRetriever {
val currentRetrieverToMediaUri = retrieverToMediaUri
return if (currentRetrieverToMediaUri == null || currentRetrieverToMediaUri.mediaUri != mediaUri) {
currentRetrieverToMediaUri?.retriever?.release()
val newRetriever = MediaMetadataRetriever().apply {
setDataSource(context, mediaUri)
}
retrieverToMediaUri = RetrieverToMediaUri(newRetriever, mediaUri)
newRetriever
} else {
currentRetrieverToMediaUri.retriever
}
}
override fun extract(params: FrameExtractParameters, listener: FrameExtractBehaviorFrameListener): Boolean {
val completed = when (params.mode) {
FrameExtractMode.Fast -> {
retrieveFrame(params, MediaMetadataRetriever.OPTION_CLOSEST_SYNC, listener)
}
FrameExtractMode.Exact -> {
retrieveFrame(params, MediaMetadataRetriever.OPTION_CLOSEST, listener)
}
}
return completed
}
override fun release() {
retrieverToMediaUri?.retriever?.release()
}
private fun retrieveFrame(params: FrameExtractParameters, retrieverOptions: Int, listener: FrameExtractBehaviorFrameListener): Boolean {
val retriever = setupRetriever(params.mediaUri)
val extractedBitmap = retriever.getFrameAtTime(params.timestampUs, retrieverOptions)
if (extractedBitmap != null) {
listener.onFrameExtracted(extractedBitmap)
} else {
listener.onFrameFailed()
}
return true
}
data class RetrieverToMediaUri(val retriever: MediaMetadataRetriever, val mediaUri: Uri)
}
| bsd-2-clause | c57522d0dc29b0538892728156cb469c | 34.914286 | 140 | 0.710422 | 4.978218 | false | false | false | false |
linkedin/LiTr | litr/src/main/java/com/linkedin/android/litr/render/GlFramebuffer.kt | 1 | 2402 | /*
* MIT License
*
* Copyright (c) 2019 Mattia Iavarone
* Copyright (c) 2021 LinkedIn Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// modified from: https://github.com/natario1/Egloo
package com.linkedin.android.litr.render
import android.opengl.GLES20
class GlFramebuffer {
val id: Int
init {
val array = IntArray(1)
GLES20.glGenFramebuffers(1, array, 0)
GlRenderUtils.checkGlError("glGenFramebuffers GlFramebuffer")
id = array[0]
}
@JvmOverloads
fun attachTexture(textureId: Int, texTarget: Int = GLES20.GL_TEXTURE_2D, attachment: Int = GLES20.GL_COLOR_ATTACHMENT0, level: Int = 0) {
GLES20.glFramebufferTexture2D(
GLES20.GL_FRAMEBUFFER,
attachment,
texTarget,
textureId,
level
)
GlRenderUtils.checkGlError("glFramebufferTexture2D GlFramebuffer")
val status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER)
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
throw RuntimeException("Bad status glCheckFramebufferStatus: $status")
}
}
fun bind() {
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, id)
}
fun unbind() {
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0)
}
fun delete() {
GLES20.glDeleteFramebuffers(1, intArrayOf(id), 0)
}
}
| bsd-2-clause | 72af54c930acf5526fedf61ea91da366 | 34.850746 | 141 | 0.706078 | 4.191972 | false | false | false | false |
AntonovAlexander/activecore | designs/coregen/agenda/src/decoder.kt | 1 | 21104 | /*
* decoder.kt
*
* Created on: 01.04.2021
* Author: Alexander Antonov <[email protected]>
* License: See LICENSE file for details
*/
package agenda
import hwast.*
// op0 sources
val OP0_SRC_RS = 0
val OP0_SRC_IMM = 1
val OP0_SRC_PC = 2
// op1 sources
val OP1_SRC_RS = 0
val OP1_SRC_IMM = 1
val OP1_SRC_CSR = 2
// ALU opcodes
val aluop_ADD = 0
val aluop_SUB = 1
val aluop_AND = 2
val aluop_OR = 3
val aluop_SLL = 4
val aluop_SRL = 5
val aluop_SRA = 6
val aluop_XOR = 7
val aluop_CLRB = 8
// M ext opcodes
val aluop_MUL = 0
val aluop_MULH = 1
val aluop_MULHSU = 2
val aluop_MULHU = 3
val aluop_DIV = 4
val aluop_DIVU = 5
val aluop_REM = 6
val aluop_REMU = 7
// jmp sources
val JMP_SRC_IMM = 0
val JMP_SRC_ALU = 1
// rd sources
val RD_ALU = 0
val RD_LUI = 1
val RD_CF_COND = 2
val RD_OF_COND = 3
val RD_PC_INC = 4
val RD_MEM = 5
val RD_CSR = 6
internal class RISCV_Decoder : reordex.RISCDecoder(CFG) {
//// base opcodes ///////////
val opcode_LOAD = 0x03
val opcode_LOAD_FP = 0x07
val opcode_MISC_MEM = 0x0f
val opcode_OP_IMM = 0x13
val opcode_AUIPC = 0x17
val opcode_OP_IMM_32 = 0x1b
val opcode_STORE = 0x23
val opcode_STORE_FP = 0x27
val opcode_AMO = 0x2f
val opcode_OP = 0x33
val opcode_LUI = 0x37
val opcode_OP_32 = 0x3b
val opcode_MADD = 0x43
val opcode_MSUB = 0x47
val opcode_NMSUB = 0x4b
val opcode_NMADD = 0x4f
val opcode_OP_FP = 0x53
val opcode_BRANCH = 0x63
val opcode_JALR = 0x67
val opcode_JAL = 0x6f
val opcode_SYSTEM = 0x73
val opcode_custom_0 = 0x0b
val instrcode_MRET = 0x30200073
///////////////////
var opcode = ugenvar("opcode", 6, 0, "0")
var funct3 = ugenvar("funct3", 2, 0, "0")
var funct7 = ugenvar("funct7", 6, 0, "0")
var immediate_I = ugenvar("immediate_I", 31, 0, "0")
var immediate_S = ugenvar("immediate_S", 31, 0, "0")
var immediate_B = ugenvar("immediate_B", 31, 0, "0")
var immediate_U = ugenvar("immediate_U", 31, 0, "0")
var immediate_J = ugenvar("immediate_J", 31, 0, "0")
var shamt = ugenvar("shamt", 4, 0, "0")
var zimm = ugenvar("zimm", 4, 0, "0")
var op0_source = ugenvar("op0_source", 1, 0, OP0_SRC_RS.toString())
var op1_source = ugenvar("op1_source", 1, 0, OP1_SRC_RS.toString())
init {
var rs0 = rsctrls[CFG.src0]!!
var rs1 = rsctrls[CFG.src1]!!
var rd = rdctrls[CFG.rd]!!
opcode.assign(instr_code[6, 0])
assign(CFG.exu_unsigned, 0)
rs0.addr.assign(instr_code[19, 15])
rs1.addr.assign(instr_code[24, 20])
rd.addr.assign(instr_code[11, 7])
funct3.assign(instr_code[14, 12])
funct7.assign(instr_code[31, 25])
shamt.assign(instr_code[24, 20])
pred.assign(instr_code[27, 24])
succ.assign(instr_code[23, 20])
csrnum.assign(instr_code[31, 20])
zimm.assign(instr_code[19, 15])
immediate_I.assign(signext(instr_code[31, 20], 32))
var immediate_S_src = ArrayList<hw_param>()
immediate_S_src.add(instr_code[31, 25])
immediate_S_src.add(instr_code[11, 7])
immediate_S.assign(signext(cnct(immediate_S_src), 32))
var immediate_B_src = ArrayList<hw_param>()
immediate_B_src.add(instr_code[31])
immediate_B_src.add(instr_code[7])
immediate_B_src.add(instr_code[30, 25])
immediate_B_src.add(instr_code[11, 8])
immediate_B_src.add(hw_imm(1, "0"))
immediate_B.assign(signext(cnct(immediate_B_src), 32))
var immediate_U_src = ArrayList<hw_param>()
immediate_U_src.add(instr_code[31, 12])
immediate_U_src.add(hw_imm(12, "0"))
immediate_U.assign(cnct(immediate_U_src))
var immediate_J_src = ArrayList<hw_param>()
immediate_J_src.add(instr_code[31])
immediate_J_src.add(instr_code[19, 12])
immediate_J_src.add(instr_code[20])
immediate_J_src.add(instr_code[30, 21])
immediate_J_src.add(hw_imm(1, "0"))
immediate_J.assign(signext(cnct(immediate_J_src), 32))
assign(exu_id, GetExuID("INTEGER"))
begcase(opcode)
run {
begbranch(opcode_LUI)
run {
op0_source.assign(OP0_SRC_IMM)
rd.req.assign(1)
rd.source.assign(RD_LUI)
immediate.assign(immediate_U)
}
endbranch()
begbranch(opcode_AUIPC)
run {
op0_source.assign(OP0_SRC_PC)
op1_source.assign(OP1_SRC_IMM)
exu_req.assign(1)
CFG.exu_opcode.assign(aluop_ADD)
rd.req.assign(1)
rd.source.assign(RD_ALU)
immediate.assign(immediate_U)
}; endbranch()
begbranch(opcode_JAL)
run {
op0_source.assign(OP0_SRC_PC)
op1_source.assign(OP1_SRC_IMM)
cf_can_alter.assign(1)
exu_req.assign(1)
assign(exu_id, GetExuID("BRANCH"))
CFG.exu_opcode.assign(aluop_ADD)
rd.req.assign(1)
rd.source.assign(RD_PC_INC)
CFG.brctrl_src.assign(JMP_SRC_ALU)
immediate.assign(immediate_J)
}; endbranch()
begbranch(opcode_JALR)
run {
rs0.req.assign(1)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_IMM)
cf_can_alter.assign(1)
exu_req.assign(1)
assign(exu_id, GetExuID("BRANCH"))
CFG.exu_opcode.assign(aluop_ADD)
rd.req.assign(1)
rd.source.assign(RD_PC_INC)
CFG.brctrl_src.assign(JMP_SRC_ALU)
immediate.assign(immediate_I)
}; endbranch()
begbranch(opcode_BRANCH)
run {
rs0.req.assign(1)
rs1.req.assign(1)
cf_can_alter.assign(1)
exu_req.assign(1)
assign(exu_id, GetExuID("BRANCH"))
CFG.exu_opcode.assign(aluop_SUB)
CFG.brctrl_cond.assign(1)
CFG.brctrl_src.assign(JMP_SRC_ALU)
immediate.assign(immediate_B)
begif(bor(eq2(funct3, 0x6), eq2(funct3, 0x7)))
run {
assign(CFG.exu_unsigned, 1)
}; endif()
}; endbranch()
begbranch(opcode_LOAD)
run {
rs0.req.assign(1)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_IMM)
rd.req.assign(1)
rd.source.assign(RD_MEM)
exu_req.assign(1)
memctrl.req.assign(1)
memctrl.we.assign(0)
immediate.assign(immediate_I)
}; endbranch()
begbranch(opcode_STORE)
run {
rs0.req.assign(1)
rs1.req.assign(1)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_IMM)
exu_req.assign(1)
memctrl.req.assign(1)
memctrl.we.assign(1)
immediate.assign(immediate_S)
}; endbranch()
begbranch(opcode_OP_IMM)
run {
rs0.req.assign(1)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_IMM)
rd.req.assign(1)
immediate.assign(immediate_I)
exu_req.assign(1)
begcase(funct3)
run {
// ADDI
begbranch(0x0)
run {
CFG.exu_opcode.assign(aluop_ADD)
rd.source.assign(RD_ALU)
}; endbranch()
// SLLI
begbranch(0x1)
run {
CFG.exu_opcode.assign(aluop_SLL)
rd.source.assign(RD_ALU)
immediate.assign(zeroext(instr_code[24, 20], 32))
}; endbranch()
// SLTI
begbranch(0x2)
run {
CFG.exu_opcode.assign(aluop_SUB)
rd.source.assign(RD_CF_COND)
}; endbranch()
// SLTIU
begbranch(0x3)
run {
CFG.exu_opcode.assign(aluop_SUB)
assign(CFG.exu_unsigned, 1)
rd.source.assign(RD_CF_COND)
}; endbranch()
// XORI
begbranch(0x4)
run {
CFG.exu_opcode.assign(aluop_XOR)
rd.source.assign(RD_ALU)
}; endbranch()
// SRLI, SRAI
begbranch(0x5)
run {
// SRAI
begif(instr_code[30])
run {
CFG.exu_opcode.assign(aluop_SRA)
}; endif()
// SRLI
begelse()
run {
CFG.exu_opcode.assign(aluop_SRL)
}; endif()
rd.source.assign(RD_ALU)
immediate.assign(zeroext(instr_code[24, 20], 32))
}; endbranch()
// ORI
begbranch(0x6)
run {
CFG.exu_opcode.assign(aluop_OR)
rd.source.assign(RD_ALU)
}; endbranch()
// ANDI
begbranch(0x7)
run {
CFG.exu_opcode.assign(aluop_AND)
rd.source.assign(RD_ALU)
}; endbranch()
}; endcase()
}; endbranch()
begbranch(opcode_OP)
run {
rs0.req.assign(1)
rs1.req.assign(1)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_RS)
rd.req.assign(1)
rd.source.assign(RD_ALU)
exu_req.assign(1)
begcase(funct3)
run {
// ADD/SUB
begbranch(0x0)
run {
// SUB
begif(instr_code[30])
run {
CFG.exu_opcode.assign(aluop_SUB)
}; endif()
// ADD
begelse()
run {
CFG.exu_opcode.assign(aluop_ADD)
}; endif()
rd.source.assign(RD_ALU)
}; endbranch()
// SLL
begbranch(0x1)
run {
CFG.exu_opcode.assign(aluop_SLL)
rd.source.assign(RD_ALU)
}; endbranch()
// SLT
begbranch(0x2)
run {
CFG.exu_opcode.assign(aluop_SUB)
rd.source.assign(RD_CF_COND)
}; endbranch()
// SLTU
begbranch(0x3)
run {
CFG.exu_opcode.assign(aluop_SUB)
assign(CFG.exu_unsigned, 1)
rd.source.assign(RD_CF_COND)
}; endbranch()
// XORI
begbranch(0x4)
run {
CFG.exu_opcode.assign(aluop_XOR)
rd.source.assign(RD_ALU)
}; endbranch()
// SRL/SRA
begbranch(0x5)
run {
// SRA
begif(instr_code[30])
run {
CFG.exu_opcode.assign(aluop_SRA)
}; endif()
// SRL
begelse()
run {
CFG.exu_opcode.assign(aluop_SRL)
}; endif()
rd.source.assign(RD_ALU)
}; endbranch()
// OR
begbranch(0x6)
run {
CFG.exu_opcode.assign(aluop_OR)
rd.source.assign(RD_ALU)
}; endbranch()
// AND
begbranch(0x7)
run {
CFG.exu_opcode.assign(aluop_AND)
rd.source.assign(RD_ALU)
}; endbranch()
}; endcase()
if (CFG.M_Ext) {
COMMENT("M extension decoding")
begif(eq2(funct7, 1))
run {
assign(exu_id, GetExuID("MUL_DIV"))
rd.source.assign(RD_ALU)
CFG.exu_opcode.assign(funct3)
}; endif()
}
}; endbranch()
begbranch(opcode_MISC_MEM)
run {
fencereq.assign(1)
}; endbranch()
begbranch(opcode_SYSTEM)
run {
begcase(funct3)
run {
// EBREAK/ECALL
begbranch(0x0)
run {
// EBREAK
begif(instr_code[20])
run {
ebreakreq.assign(1)
}; endif()
// ECALL
begelse()
run {
ecallreq.assign(1)
}; endif()
}; endbranch()
//CSRRW
begbranch(0x1)
run {
csrreq.assign(1)
rs0.req.assign(1)
rd.req.assign(1)
rd.source.assign(RD_CSR)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_CSR)
}; endbranch()
// CSRRS
begbranch(0x2)
run {
csrreq.assign(1)
rs0.req.assign(1)
rd.req.assign(1)
rd.source.assign(RD_CSR)
exu_req.assign(1)
CFG.exu_opcode.assign(aluop_OR)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_CSR)
}; endbranch()
// CSRRC
begbranch(0x3)
run {
csrreq.assign(1)
rs0.req.assign(1)
rd.req.assign(1)
rd.source.assign(RD_CSR)
exu_req.assign(1)
CFG.exu_opcode.assign(aluop_CLRB)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_CSR)
}; endbranch()
// CSRRWI
begbranch(0x5)
run {
csrreq.assign(1)
rd.req.assign(1)
op0_source.assign(OP0_SRC_IMM)
op1_source.assign(OP1_SRC_CSR)
immediate.assign(zeroext(zimm, 32))
}; endbranch()
// CSRRSI
begbranch(0x6)
run {
csrreq.assign(1)
rd.req.assign(1)
rd.source.assign(RD_CSR)
exu_req.assign(1)
CFG.exu_opcode.assign(aluop_CLRB)
op0_source.assign(OP0_SRC_IMM)
op1_source.assign(OP1_SRC_CSR)
immediate.assign(zeroext(zimm, 32))
}; endbranch()
// CSRRSI
begbranch(0x7)
run {
csrreq.assign(1)
rd.req.assign(1)
rd.source.assign(RD_CSR)
exu_req.assign(1)
CFG.exu_opcode.assign(aluop_CLRB)
op0_source.assign(OP0_SRC_IMM)
op1_source.assign(OP1_SRC_CSR)
immediate.assign(zeroext(zimm, 32))
}; endbranch()
}; endcase()
}; endbranch()
if (CFG.Custom0_Ext) {
begbranch(opcode_custom_0)
run {
assign(exu_id, GetExuID("CUSTOM_0"))
CFG.exu_opcode.assign(funct3)
// assumed: R-type
rs0.req.assign(1)
rs1.req.assign(1)
op0_source.assign(OP0_SRC_RS)
op1_source.assign(OP1_SRC_RS)
rd.req.assign(1)
}; endbranch()
}
}; endcase()
CFG.curinstraddr_imm.assign(curinstr_addr + immediate)
begif(memctrl.req)
run {
begcase(funct3)
run {
begbranch(0x0)
run {
memctrl.be.assign(0x1)
memctrl.load_signext.assign(1)
}; endbranch()
begbranch(0x1)
run {
memctrl.be.assign(0x3)
memctrl.load_signext.assign(1)
}; endbranch()
begbranch(0x2)
run {
memctrl.be.assign(0xf)
}; endbranch()
begbranch(0x4)
run {
memctrl.be.assign(0x1)
}; endbranch()
begbranch(0x5)
run {
memctrl.be.assign(0x3)
}; endbranch()
}; endcase()
}; endif()
CFG.brctrl_mask.assign(funct3)
assign(CFG.exu_rd_source, rd.source)
begif(eq2(instr_code, instrcode_MRET))
run {
mret_req.assign(1)
cf_can_alter.assign(1)
exu_req.assign(1)
assign(exu_id, GetExuID("BRANCH"))
CFG.brctrl_src.assign(JMP_SRC_IMM)
op0_source.assign(OP0_SRC_IMM)
immediate.assign(MRETADDR)
}; endif()
begif(eq2(rs0.addr, 0))
run {
rs0.req.assign(0)
}; endif()
begif(eq2(rs1.addr, 0))
run {
rs1.req.assign(0)
}; endif()
begif(eq2(rd.addr, 0))
run {
rd.req.assign(0)
}; endif()
////
begif(csrreq)
run {
csr_rdata.assign(CSR_MCAUSE)
}; endif()
begcase(op0_source)
run {
begbranch(OP0_SRC_RS)
run {
begif(eq2(rs0.addr, 0))
run {
SrcSetImm(CFG.src0, hw_imm(0))
}; endif()
begelse()
run {
SrcReadReg(CFG.src0, rs0.addr)
}; endif()
}; endbranch()
begbranch(OP0_SRC_IMM)
run {
SrcSetImm(CFG.src0, immediate)
}; endbranch()
begbranch(OP0_SRC_PC)
run {
SrcSetImm(CFG.src0, curinstr_addr)
}; endbranch()
}; endcase()
// TODO: cleanup
begif(rs1.req)
run {
begif(eq2(rs1.addr, 0))
run {
SrcSetImm(CFG.src1, hw_imm(0))
}; endif()
begelse()
run {
SrcReadReg(CFG.src1, rs1.addr)
}; endif()
}; endif()
begif (!memctrl.req)
run {
begcase(op1_source)
run {
begbranch(OP1_SRC_IMM)
run {
SrcSetImm(CFG.src1, immediate)
}; endbranch()
begbranch(OP1_SRC_CSR)
run {
SrcSetImm(CFG.src1, csr_rdata)
}; endbranch()
}; endcase()
}; endif()
}
} | apache-2.0 | 4d1b503e72b0e6ab8b36af8d42d84a01 | 29.63135 | 76 | 0.406937 | 3.824574 | false | false | false | false |
pureal-code/pureal-os | traits/src/net/pureal/traits/math/sets/SetIntersection.kt | 1 | 3245 | package net.pureal.traits.math.sets
import net.pureal.traits.math.*
import net.pureal.traits.Constructor2
public trait SetIntersection : net.pureal.traits.math.Set {
companion object : Constructor2<SetIntersection, net.pureal.traits.math.Set, net.pureal.traits.math.Set> {
override fun invoke(ss1: net.pureal.traits.math.Set, ss2: net.pureal.traits.math.Set): SetIntersection = object : SetIntersection {
override val superset1: net.pureal.traits.math.Set = ss1
override val superset2: net.pureal.traits.math.Set = ss2
}
}
val superset1: net.pureal.traits.math.Set
val superset2: net.pureal.traits.math.Set
override fun toString(): String = "setIntersection(${superset1},${superset2})"
fun simplifySets(): net.pureal.traits.math.Set {
var s_ss1: net.pureal.traits.math.Set = superset1
var s_ss2: net.pureal.traits.math.Set = superset2
when (s_ss1) {
is SetIntersection -> s_ss1 = (s_ss1 as SetIntersection).simplifySets()
is SetUnion -> s_ss1 = (s_ss1 as SetIntersection).simplifySets()
is EmptySet -> return EmptySet
}
when (s_ss2) {
is SetIntersection -> s_ss2 = (s_ss2 as SetIntersection).simplifySets()
is SetUnion -> s_ss2 = (s_ss1 as SetIntersection).simplifySets()
is EmptySet -> return EmptySet
}
if (s_ss1 !is RealSet || s_ss2 !is RealSet) return setIntersection(s_ss1, s_ss2)
val t_ss1: RealSet = s_ss1 as RealSet
val t_ss2: RealSet = s_ss2 as RealSet
val le: Calculatable
val he: Calculatable
val lc: Boolean
val hc: Boolean
if (t_ss1.lowEnd == t_ss2.lowEnd) {
le = t_ss1.lowEnd
lc = t_ss1.lowClosed && t_ss2.lowClosed
} else if (t_ss1.lowEnd < t_ss2.lowEnd) {
le = t_ss2.lowEnd
lc = t_ss2.lowClosed
} else {
le = t_ss1.lowEnd
lc = t_ss1.lowClosed
}
if (t_ss1.highEnd == t_ss2.highEnd) {
he = t_ss1.highEnd
hc = t_ss1.highClosed && t_ss2.highClosed
} else if (t_ss1.highEnd > t_ss2.highEnd) {
he = t_ss2.highEnd
hc = t_ss2.highClosed
} else {
he = t_ss1.highEnd
hc = t_ss1.highClosed
}
if (le > he) return EmptySet
if (le == he && !(lc && hc)) return EmptySet
var f: Calculatable? = null
if (t_ss1 is MultipleOfSet) f = t_ss1.factor
if (t_ss2 is MultipleOfSet) f = if (f == null) t_ss2.factor; else t_ss2.factor * f
if (f == null) return realSet(le, he, lc, hc)
return multipleOfSet(f!!, realSet(le, he, lc, hc))
}
override fun hasCommonElementsWith(other: net.pureal.traits.math.Set): Boolean {
//TODO
return false
}
override fun contains(other: Number): Boolean {
return other in superset1 && other in superset2
}
override fun equals(other: Any?): Boolean = other is SetIntersection &&
((superset1 == other.superset1 && superset2 == other.superset2) || (superset2 == other.superset1 && superset1 == other.superset2))
}
val setIntersection = SetIntersection | bsd-3-clause | e018c263c57101795e19db88a99caea2 | 37.642857 | 142 | 0.597843 | 3.387265 | false | false | false | false |
dhis2/dhis2-android-sdk | core/src/main/java/org/hisp/dhis/android/core/program/programindicatorengine/internal/ProgramIndicatorEngineImpl.kt | 1 | 7416 | /*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.android.core.program.programindicatorengine.internal
import java.util.*
import javax.inject.Inject
import org.hisp.dhis.android.core.arch.db.stores.internal.IdentifiableObjectStore
import org.hisp.dhis.android.core.arch.helpers.UidsHelper.mapByUid
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScope
import org.hisp.dhis.android.core.constant.Constant
import org.hisp.dhis.android.core.dataelement.DataElement
import org.hisp.dhis.android.core.enrollment.Enrollment
import org.hisp.dhis.android.core.enrollment.internal.EnrollmentStore
import org.hisp.dhis.android.core.event.Event
import org.hisp.dhis.android.core.event.EventCollectionRepository
import org.hisp.dhis.android.core.program.ProgramIndicator
import org.hisp.dhis.android.core.program.ProgramStage
import org.hisp.dhis.android.core.program.ProgramStageCollectionRepository
import org.hisp.dhis.android.core.program.programindicatorengine.ProgramIndicatorEngine
import org.hisp.dhis.android.core.trackedentity.TrackedEntityAttribute
import org.hisp.dhis.android.core.trackedentity.TrackedEntityAttributeValue
import org.hisp.dhis.android.core.trackedentity.internal.TrackedEntityAttributeValueStore
internal class ProgramIndicatorEngineImpl @Inject constructor(
private val programIndicatorStore: IdentifiableObjectStore<ProgramIndicator>,
private val dataElementStore: IdentifiableObjectStore<DataElement>,
private val trackedEntityAttributeStore: IdentifiableObjectStore<TrackedEntityAttribute>,
private val enrollmentStore: EnrollmentStore,
private val eventRepository: EventCollectionRepository,
private val programRepository: ProgramStageCollectionRepository,
private val trackedEntityAttributeValueStore: TrackedEntityAttributeValueStore,
private val constantStore: IdentifiableObjectStore<Constant>,
private val programStageStore: IdentifiableObjectStore<ProgramStage>
) : ProgramIndicatorEngine {
override fun getProgramIndicatorValue(
enrollmentUid: String?,
eventUid: String?,
programIndicatorUid: String
): String? {
return when {
eventUid != null -> getEventProgramIndicatorValue(eventUid, programIndicatorUid)
enrollmentUid != null -> getEnrollmentProgramIndicatorValue(enrollmentUid, programIndicatorUid)
else -> return null
}
}
override fun getEnrollmentProgramIndicatorValue(enrollmentUid: String, programIndicatorUid: String): String? {
val programIndicator = programIndicatorStore.selectByUid(programIndicatorUid) ?: return null
val enrollment = enrollmentStore.selectByUid(enrollmentUid)
?: throw NoSuchElementException("Enrollment $enrollmentUid does not exist.")
val programIndicatorContext = ProgramIndicatorContext(
programIndicator = programIndicator,
attributeValues = getAttributeValues(enrollment),
enrollment = enrollment,
events = getEnrollmentEvents(enrollment)
)
return evaluateProgramIndicatorContext(programIndicatorContext)
}
override fun getEventProgramIndicatorValue(eventUid: String, programIndicatorUid: String): String? {
val programIndicator = programIndicatorStore.selectByUid(programIndicatorUid) ?: return null
val event = eventRepository
.withTrackedEntityDataValues()
.byDeleted().isFalse
.uid(eventUid)
.blockingGet() ?: throw NoSuchElementException("Event $eventUid does not exist or is deleted.")
val enrollment = event.enrollment()?.let {
enrollmentStore.selectByUid(it)
}
val programIndicatorContext = ProgramIndicatorContext(
programIndicator = programIndicator,
attributeValues = getAttributeValues(enrollment),
enrollment = enrollment,
events = mapOf(event.programStage()!! to listOf(event))
)
return evaluateProgramIndicatorContext(programIndicatorContext)
}
private fun evaluateProgramIndicatorContext(context: ProgramIndicatorContext): String? {
val executor = ProgramIndicatorExecutor(
constantMap,
context,
dataElementStore,
trackedEntityAttributeStore,
programStageStore
)
return executor.getProgramIndicatorValue(context.programIndicator)
}
private val constantMap: Map<String, Constant>
get() {
val constants = constantStore.selectAll()
return mapByUid(constants)
}
private fun getAttributeValues(enrollment: Enrollment?): Map<String, TrackedEntityAttributeValue> {
val trackedEntityAttributeValues = enrollment?.trackedEntityInstance()?.let { teiUid ->
trackedEntityAttributeValueStore.queryByTrackedEntityInstance(teiUid)
} ?: return mapOf()
return trackedEntityAttributeValues
.filter { it.trackedEntityAttribute() != null }
.map { it.trackedEntityAttribute()!! to it }.toMap()
}
private fun getEnrollmentEvents(enrollment: Enrollment?): Map<String, List<Event>> {
val programStageUids = programRepository.byProgramUid().eq(enrollment!!.program()).blockingGetUids()
return programStageUids.map { programStageUid ->
val programStageEvents = eventRepository
.byProgramStageUid().eq(programStageUid)
.byEnrollmentUid().eq(enrollment.uid())
.byDeleted().isFalse
.orderByEventDate(RepositoryScope.OrderByDirection.ASC)
.orderByLastUpdated(RepositoryScope.OrderByDirection.ASC)
.withTrackedEntityDataValues()
.blockingGet()
programStageUid to programStageEvents
}.toMap()
}
}
| bsd-3-clause | 17eb28501b96b91f94c73fa8152060b2 | 46.538462 | 114 | 0.739482 | 5.350649 | false | false | false | false |
cdietze/klay | klay-scene/src/main/kotlin/klay/scene/ClippedLayer.kt | 1 | 2752 | package klay.scene
import euklid.f.IDimension
import euklid.f.Point
import euklid.f.Vector
import klay.core.Surface
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
/**
* A layer whose rendering is (usually) clipped to a rectangle. The clipping rectangle is defined
* to be the layer's `x, y` coordinate (as adjusted by its origin) extended to the layer's
* scaled width and height.
*
* NOTE: clipping rectangles cannot be rotated. If the layer has a rotation, the clipping region
* will be undefined (and most certainly wacky).
*/
abstract class ClippedLayer(private var width: Float, private var height: Float) : Layer() {
private val pos = Point()
private val size = Vector()
override fun width(): Float {
return this.width
}
override fun height(): Float {
return this.height
}
/** Updates the size of this clipped layer, and hence its clipping rectangle. */
fun setSize(width: Float, height: Float): ClippedLayer {
this.width = width
this.height = height
checkOrigin()
return this
}
/** Updates the size of this clipped layer, and hence its clipping rectangle. */
fun setSize(size: IDimension): ClippedLayer {
return setSize(size.width, size.height)
}
/** Updates the width of this group layer, and hence its clipping rectangle. */
fun setWidth(width: Float): ClippedLayer {
this.width = width
checkOrigin()
return this
}
/** Updates the height of this group layer, and hence its clipping rectangle. */
fun setHeight(height: Float): ClippedLayer {
this.height = height
checkOrigin()
return this
}
protected open fun disableClip(): Boolean {
return false
}
override fun paintImpl(surf: Surface) {
if (disableClip())
paintClipped(surf)
else {
val tx = surf.tx()
val originX = originX()
val originY = originY()
tx.translate(originX, originY)
tx.transform(pos.set(-originX, -originY), pos)
tx.transform(size.set(width, height), size)
tx.translate(-originX, -originY)
val nonEmpty = surf.startClipped(
pos.x.toInt(), pos.y.toInt(), size.x.absoluteValue.roundToInt(), size.y.absoluteValue.roundToInt())
try {
if (nonEmpty) paintClipped(surf)
} finally {
surf.endClipped()
}
}
}
/**
* Renders this layer with the clipping region in effect. NOTE: this layer's transform will
* already have been applied to the surface.
*/
protected abstract fun paintClipped(surf: Surface)
}
| apache-2.0 | be6696e00e81350bdc5dcf9de208a89a | 29.921348 | 119 | 0.625363 | 4.389155 | false | false | false | false |
elpassion/el-space-android | el-debate/src/testCommon/java/pl/elpassion/elspace/dabate/details/DebateDataFactory.kt | 1 | 912 | package pl.elpassion.elspace.dabate.details
import pl.elpassion.elspace.debate.details.*
fun createDebateData(debateTopic: String = "topic", answers: Answers = createAnswers(), lastAnswerId: Long? = -1)
= DebateData(debateTopic, answers, lastAnswerId)
fun createAnswers(positiveAnswer: Positive = createPositiveAnswer(),
negativeAnswer: Negative = createNegativeAnswer(),
neutralAnswer: Neutral = createNeutralAnswer())
= Answers(positiveAnswer, negativeAnswer, neutralAnswer)
fun createPositiveAnswer(answerId: Long = 1, answerLabel: String = "answerPositive")
= Positive(answerId, answerLabel)
fun createNegativeAnswer(answerId: Long = 2, answerLabel: String = "answerNegative")
= Negative(answerId, answerLabel)
fun createNeutralAnswer(answerId: Long = 3, answerLabel: String = "answerNeutral")
= Neutral(answerId, answerLabel) | gpl-3.0 | 1669ca1e26fb6645f62e3cf0f58d994f | 44.65 | 113 | 0.730263 | 4.108108 | false | false | false | false |
shadowfox-ninja/Bookmark-d | app/src/main/java/tech/shadowfox/bookmarkd/common/FragmentTransaction.kt | 1 | 4082 | package tech.shadowfox.bookmarkd.common
import android.support.annotation.AnimRes
import android.support.v4.app.FragmentManager
import kotlin.properties.Delegates
/**
* Created on 6/10/17.
*/
class FragmentTransaction(private val fragment: ShadowLifecycleFragment, private val manager: FragmentManager) {
private var twoWayAnimation: TwoWayFragmentAnimation? = null
private var oneWayAnimation: OneWayFragmentAnimation? = null
private var transitionType: Int? = null
private var transitionResource: Int? = null
private var backstack_tag: String? = null
private var tag: String? = null
private var container: Int by Delegates.notNull()
private var extraLogic: (android.support.v4.app.FragmentTransaction) -> android.support.v4.app.FragmentTransaction = { it }
fun into(container: Int): FragmentTransaction {
this.container = container
return this
}
fun extraLogic(extraLogic: (android.support.v4.app.FragmentTransaction) -> android.support.v4.app.FragmentTransaction): FragmentTransaction {
this.extraLogic = extraLogic
return this
}
fun withAnimation(animation: TwoWayFragmentAnimation): FragmentTransaction {
oneWayAnimation = null
twoWayAnimation = animation
return this
}
fun withTransition(transition: Int): FragmentTransaction {
transitionResource = null
transitionType = transition
return this
}
fun withTransitionStyle(transition: Int): FragmentTransaction {
transitionResource = transition
transitionType = null
return this
}
fun withAnimation(animation: OneWayFragmentAnimation): FragmentTransaction {
twoWayAnimation = null
oneWayAnimation = animation
return this
}
fun android.support.v4.app.FragmentTransaction.extraLogic(): android.support.v4.app.FragmentTransaction = extraLogic(this)
fun addFragment() {
manager.beginTransaction()
.extraLogic()
.hide(manager.findFragmentById(container)).apply {
if (tag != null) { add(container, fragment, tag) } else { add(container, fragment) }
twoWayAnimation?.let { setCustomAnimations(it.enter, it.exit, it.popEnter, it.popExit) }
oneWayAnimation?.let { setCustomAnimations(it.enter, it.exit) }
transitionType?.let { setTransition(it) }
transitionResource?.let { setTransitionStyle(it) }
}.addToBackStack(backstack_tag).commitAllowingStateLoss()
manager.executePendingTransactions()
}
fun switchFragment() {
manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
manager.beginTransaction().replace(container, fragment).apply {
if (tag != null) replace(container, fragment, tag) else replace(container, fragment)
twoWayAnimation?.let { setCustomAnimations(it.enter, it.exit, it.popEnter, it.popExit) }
oneWayAnimation?.let { setCustomAnimations(it.enter, it.exit) }
transitionType?.let { setTransition(it) }
transitionResource?.let { setTransitionStyle(it) }
}.commit()
manager.executePendingTransactions()
}
}
data class TwoWayFragmentAnimation(@AnimRes val enter: Int, @AnimRes val exit: Int, @AnimRes val popEnter: Int, @AnimRes val popExit: Int)
data class OneWayFragmentAnimation(@AnimRes val enter: Int, @AnimRes val exit: Int)
fun ShadowLifecycleFragment.withManager(manager: FragmentManager, container: Int): FragmentTransaction {
return FragmentTransaction(this, manager).into(container)
}
fun ShadowLifecycleFragment.withManager(activity: ShadowLifecycleFragmentActivity): FragmentTransaction {
return FragmentTransaction(this, activity.supportFragmentManager).into(activity.defaultFragContainer)
}
fun ShadowLifecycleFragment.withManager(fragment: ShadowLifecycleFragment): FragmentTransaction {
return FragmentTransaction(this, fragment.activity.supportFragmentManager)
.into(fragment.activityContract.defaultFragContainer)
} | mit | ec3134ba08b0c6becf01e140f1406483 | 41.092784 | 145 | 0.720235 | 4.802353 | false | false | false | false |
paplorinc/intellij-community | uast/uast-common/src/org/jetbrains/uast/UastLanguagePlugin.kt | 1 | 5065 | // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.uast
import com.intellij.lang.Language
import com.intellij.openapi.extensions.ExtensionPointName
import com.intellij.psi.*
interface UastLanguagePlugin {
companion object {
val extensionPointName = ExtensionPointName<UastLanguagePlugin>("org.jetbrains.uast.uastLanguagePlugin")
private val extensions by lazy(LazyThreadSafetyMode.PUBLICATION) { extensionPointName.extensionList }
fun getInstances(): Collection<UastLanguagePlugin> = extensions
fun byLanguage(language: Language): UastLanguagePlugin? = extensions.firstOrNull { it.language === language }
}
data class ResolvedMethod(val call: UCallExpression, val method: PsiMethod)
data class ResolvedConstructor(val call: UCallExpression, val constructor: PsiMethod, val clazz: PsiClass)
val language: Language
/**
* Checks if the file with the given [fileName] is supported.
*
* @param fileName the source file name.
* @return true, if the file is supported by this converter, false otherwise.
*/
fun isFileSupported(fileName: String): Boolean
/**
* Returns the converter priority. Might be positive, negative or 0 (Java's is 0).
* UastConverter with the higher priority will be queried earlier.
*
* Priority is useful when a language N wraps its own elements (NElement) to, for example, Java's PsiElements,
* and Java resolves the reference to such wrapped PsiElements, not the original NElement.
* In this case N implementation can handle such wrappers in UastConverter earlier than Java's converter,
* so N language converter will have a higher priority.
*/
val priority: Int
/**
* Converts a PSI element, the parent of which already has an UAST representation, to UAST.
*
* @param element the element to convert
* @param parent the parent as an UAST element, or null if the element is a file
* @param requiredType the expected type of the result.
* @return the converted element, or null if the element isn't supported or doesn't match the required result type.
*/
fun convertElement(element: PsiElement, parent: UElement?, requiredType: Class<out UElement>? = null): UElement?
/**
* Converts a PSI element, along with its chain of parents, to UAST.
*
* @param element the element to convert
* @param requiredType the expected type of the result.
* @return the converted element, or null if the element isn't supported or doesn't match the required result type.
*/
fun convertElementWithParent(element: PsiElement, requiredType: Class<out UElement>?): UElement?
fun getMethodCallExpression(
element: PsiElement,
containingClassFqName: String?,
methodName: String
): ResolvedMethod?
fun getConstructorCallExpression(
element: PsiElement,
fqName: String
): ResolvedConstructor?
fun getMethodBody(element: PsiMethod): UExpression? {
if (element is UMethod) return element.uastBody
return (convertElementWithParent(element, null) as? UMethod)?.uastBody
}
fun getInitializerBody(element: PsiClassInitializer): UExpression {
if (element is UClassInitializer) return element.uastBody
return (convertElementWithParent(element, null) as? UClassInitializer)?.uastBody ?: UastEmptyExpression(null)
}
fun getInitializerBody(element: PsiVariable): UExpression? {
if (element is UVariable) return element.uastInitializer
return (convertElementWithParent(element, null) as? UVariable)?.uastInitializer
}
/**
* Returns true if the expression value is used.
* Do not rely on this property too much, its value can be approximate in some cases.
*/
fun isExpressionValueUsed(element: UExpression): Boolean
@JvmDefault
fun <T : UElement> convertElementWithParent(element: PsiElement, requiredTypes: Array<out Class<out T>>): T? =
when {
requiredTypes.isEmpty() -> convertElementWithParent(element, null)
requiredTypes.size == 1 -> convertElementWithParent(element, requiredTypes.single())
else -> convertElementWithParent(element, null)
?.takeIf { result -> requiredTypes.any { it.isAssignableFrom(result.javaClass) } }
} as? T
@JvmDefault
fun <T : UElement> convertToAlternatives(element: PsiElement, requiredTypes: Array<out Class<out T>>): Sequence<T> =
sequenceOf(convertElementWithParent(element, requiredTypes)).filterNotNull()
}
inline fun <reified T : UElement> UastLanguagePlugin.convertOpt(element: PsiElement?, parent: UElement?): T? {
if (element == null) return null
return convertElement(element, parent) as? T
}
inline fun <reified T : UElement> UastLanguagePlugin.convert(element: PsiElement, parent: UElement?): T {
return convertElement(element, parent, T::class.java) as T
}
inline fun <reified T : UElement> UastLanguagePlugin.convertWithParent(element: PsiElement?): T? {
if (element == null) return null
return convertElementWithParent(element, T::class.java) as? T
} | apache-2.0 | de7197660fa8de4ae9f3af6b4e32d074 | 40.867769 | 140 | 0.745706 | 4.782814 | false | false | false | false |
google/intellij-community | plugins/kotlin/fir/test/org/jetbrains/kotlin/idea/fir/shortenRefs/AbstractFirShortenRefsTest.kt | 3 | 1788 | // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.fir.shortenRefs
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.AbstractImportsTest
import org.jetbrains.kotlin.executeOnPooledThreadInReadAction
import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.test.utils.IgnoreTests
abstract class AbstractFirShortenRefsTest : AbstractImportsTest() {
override val captureExceptions: Boolean = false
override fun isFirPlugin(): Boolean = true
override fun doTest(file: KtFile): String? {
val selectionModel = myFixture.editor.selectionModel
if (!selectionModel.hasSelection()) error("No selection in input file")
val selection = runReadAction { TextRange(selectionModel.selectionStart, selectionModel.selectionEnd) }
val shortenings = executeOnPooledThreadInReadAction {
analyze(file) {
collectPossibleReferenceShortenings(file, selection)
}
}
project.executeWriteCommand("") {
shortenings.invokeShortening()
}
selectionModel.removeSelection()
return null
}
override val runTestInWriteCommand: Boolean = false
protected fun doTestWithMuting(unused: String) {
IgnoreTests.runTestIfEnabledByFileDirective(dataFile().toPath(), IgnoreTests.DIRECTIVES.FIR_COMPARISON, ".after") {
doTest(unused)
}
}
override val nameCountToUseStarImportDefault: Int
get() = Integer.MAX_VALUE
}
| apache-2.0 | 960ee7bc949a5e840c986b68a119699e | 36.25 | 123 | 0.735459 | 5.050847 | false | true | false | false |
SimpleMobileTools/Simple-Gallery | app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SetWallpaperActivity.kt | 1 | 6190 | package com.simplemobiletools.gallery.pro.activities
import android.app.Activity
import android.app.WallpaperManager
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.checkAppSideloading
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.NavigationIcon
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.gallery.pro.R
import com.theartofdev.edmodo.cropper.CropImageView
import kotlinx.android.synthetic.main.activity_set_wallpaper.*
import kotlinx.android.synthetic.main.bottom_set_wallpaper_actions.*
class SetWallpaperActivity : SimpleActivity(), CropImageView.OnCropImageCompleteListener {
private val RATIO_PORTRAIT = 0
private val RATIO_LANDSCAPE = 1
private val RATIO_SQUARE = 2
private val PICK_IMAGE = 1
private var aspectRatio = RATIO_PORTRAIT
private var wallpaperFlag = -1
lateinit var uri: Uri
lateinit var wallpaperManager: WallpaperManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_set_wallpaper)
setupBottomActions()
if (checkAppSideloading()) {
return
}
setupOptionsMenu()
if (intent.data == null) {
val pickIntent = Intent(applicationContext, MainActivity::class.java)
pickIntent.action = Intent.ACTION_PICK
pickIntent.type = "image/*"
startActivityForResult(pickIntent, PICK_IMAGE)
return
}
handleImage(intent)
}
override fun onResume() {
super.onResume()
setupToolbar(set_wallpaper_toolbar, NavigationIcon.Arrow)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
if (requestCode == PICK_IMAGE) {
if (resultCode == Activity.RESULT_OK && resultData != null) {
handleImage(resultData)
} else {
finish()
}
}
super.onActivityResult(requestCode, resultCode, resultData)
}
private fun setupOptionsMenu() {
set_wallpaper_toolbar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.save -> confirmWallpaper()
R.id.allow_changing_aspect_ratio -> crop_image_view.clearAspectRatio()
else -> return@setOnMenuItemClickListener false
}
return@setOnMenuItemClickListener true
}
}
private fun handleImage(intent: Intent) {
uri = intent.data!!
if (uri.scheme != "file" && uri.scheme != "content") {
toast(R.string.unknown_file_location)
finish()
return
}
wallpaperManager = WallpaperManager.getInstance(applicationContext)
crop_image_view.apply {
setOnCropImageCompleteListener(this@SetWallpaperActivity)
setImageUriAsync(uri)
}
setupAspectRatio()
}
private fun setupBottomActions() {
bottom_set_wallpaper_aspect_ratio.setOnClickListener {
changeAspectRatio()
}
bottom_set_wallpaper_rotate.setOnClickListener {
crop_image_view.rotateImage(90)
}
}
private fun setupAspectRatio() {
var widthToUse = wallpaperManager.desiredMinimumWidth
val heightToUse = wallpaperManager.desiredMinimumHeight
if (widthToUse == heightToUse) {
widthToUse /= 2
}
when (aspectRatio) {
RATIO_PORTRAIT -> crop_image_view.setAspectRatio(heightToUse, widthToUse)
RATIO_LANDSCAPE -> crop_image_view.setAspectRatio(widthToUse, heightToUse)
else -> crop_image_view.setAspectRatio(widthToUse, widthToUse)
}
}
private fun changeAspectRatio() {
aspectRatio = ++aspectRatio % (RATIO_SQUARE + 1)
setupAspectRatio()
}
private fun confirmWallpaper() {
if (isNougatPlus()) {
val items = arrayListOf(
RadioItem(WallpaperManager.FLAG_SYSTEM, getString(R.string.home_screen)),
RadioItem(WallpaperManager.FLAG_LOCK, getString(R.string.lock_screen)),
RadioItem(WallpaperManager.FLAG_SYSTEM or WallpaperManager.FLAG_LOCK, getString(R.string.home_and_lock_screen))
)
RadioGroupDialog(this, items) {
wallpaperFlag = it as Int
crop_image_view.getCroppedImageAsync()
}
} else {
crop_image_view.getCroppedImageAsync()
}
}
override fun onCropImageComplete(view: CropImageView?, result: CropImageView.CropResult) {
if (isDestroyed)
return
if (result.error == null) {
toast(R.string.setting_wallpaper)
ensureBackgroundThread {
val bitmap = result.bitmap
val wantedHeight = wallpaperManager.desiredMinimumHeight
val ratio = wantedHeight / bitmap.height.toFloat()
val wantedWidth = (bitmap.width * ratio).toInt()
try {
val scaledBitmap = Bitmap.createScaledBitmap(bitmap, wantedWidth, wantedHeight, true)
if (isNougatPlus()) {
wallpaperManager.setBitmap(scaledBitmap, null, true, wallpaperFlag)
} else {
wallpaperManager.setBitmap(scaledBitmap)
}
setResult(Activity.RESULT_OK)
} catch (e: OutOfMemoryError) {
toast(R.string.out_of_memory_error)
setResult(Activity.RESULT_CANCELED)
}
finish()
}
} else {
toast("${getString(R.string.image_editing_failed)}: ${result.error.message}")
}
}
}
| gpl-3.0 | 5c4a65d4a760fd0865a2397cb380ce14 | 34.780347 | 127 | 0.631018 | 4.983897 | false | false | false | false |
nielsutrecht/adventofcode | src/main/kotlin/com/nibado/projects/advent/collect/Maze.kt | 1 | 2530 | package com.nibado.projects.advent.collect
import com.nibado.projects.advent.Point
class Maze(val width: Int, val height: Int) {
companion object {
val IMPASSIBLE = Float.POSITIVE_INFINITY
val UNIT = 1.0f
}
private val array = FloatArray(width * height)
fun index(x: Int, y: Int) = x + y * width
fun get(x: Int, y: Int) = array[index(x, y)]
fun get(p: Point) = get(p.x, p.y)
fun set(x: Int, y: Int, value: Float) {
array[index(x, y)] = value
}
fun set(point: Point, value: Float) {
array[index(point.x, point.y)] = value
}
fun set(x: Int, y: Int, wall: Boolean) {
set(x, y, if (wall) IMPASSIBLE else UNIT)
}
fun set(point: Point, wall: Boolean) {
set(point.x, point.y, if (wall) IMPASSIBLE else UNIT)
}
fun isWall(x: Int, y: Int) = get(x, y) == IMPASSIBLE
fun isWall(p: Point) = get(p) == IMPASSIBLE
private fun assertInBounds(vararg points: Point) {
points.forEach {
if (!inBound(it)) {
throw IllegalArgumentException("Point $it not in bounds $width, $height")
}
}
}
fun dfs(from: Point, to: Point): List<Point> {
assertInBounds(from, to)
return dfs(to, from, mutableSetOf())
}
private fun dfs(to: Point, current: Point, visited: MutableSet<Point>): List<Point> {
if (current == to) {
return listOf(current)
}
visited += current
neighbors(current).filterNot { isWall(it) }.filterNot { visited.contains(it) }.forEach {
val result = dfs(to, it, visited)
if (result.isNotEmpty()) {
return listOf(current) + result
}
}
return listOf()
}
fun countBy(func: (Float) -> Boolean) = array.count(func)
fun countWalls() = countBy { it == IMPASSIBLE }
fun count() = countBy { it != IMPASSIBLE }
fun neighbors(point: Point) = point.neighbors().filter { inBound(it) }
fun inBound(point: Point) = point.inBound(width - 1, height - 1)
fun inBound(x: Int, y: Int) = Point(x, y).inBound(width - 1, height - 1)
fun print(path: Set<Point> = setOf()) {
for (y in 0 until height) {
for (x in 0 until width) {
if (path.contains(Point(x, y))) {
print('O')
} else {
print(if (array[index(x, y)] == IMPASSIBLE) '#' else '.')
}
}
println()
}
}
} | mit | 1883b77c45d16f9ab45131dc3b41dde8 | 27.761364 | 96 | 0.53913 | 3.568406 | false | false | false | false |
allotria/intellij-community | platform/workspaceModel/storage/src/com/intellij/workspaceModel/storage/impl/references/OneToAbstractMany.kt | 1 | 2475 | // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.workspaceModel.storage.impl.references
import com.intellij.workspaceModel.storage.impl.ConnectionId
import com.intellij.workspaceModel.storage.impl.ConnectionId.ConnectionType.ONE_TO_ABSTRACT_MANY
import com.intellij.workspaceModel.storage.impl.updateOneToAbstractManyChildrenOfParent
import com.intellij.workspaceModel.storage.impl.ModifiableWorkspaceEntityBase
import com.intellij.workspaceModel.storage.impl.WorkspaceEntityBase
import com.intellij.workspaceModel.storage.impl.extractOneToAbstractManyChildren
import kotlin.properties.ReadOnlyProperty
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty
class OneToAbstractMany<Parent : WorkspaceEntityBase, Child : WorkspaceEntityBase>(private val childClass: Class<Child>) : ReadOnlyProperty<Parent, Sequence<Child>> {
private var connectionId: ConnectionId? = null
override fun getValue(thisRef: Parent, property: KProperty<*>): Sequence<Child> {
if (connectionId == null) {
connectionId = ConnectionId.create(thisRef.javaClass, childClass, ONE_TO_ABSTRACT_MANY, true, false)
}
return thisRef.snapshot.extractOneToAbstractManyChildren(connectionId!!, thisRef.id)
}
}
class MutableOneToAbstractMany<Parent : WorkspaceEntityBase, Child : WorkspaceEntityBase, ModifParent : ModifiableWorkspaceEntityBase<Parent>>(
private val parentClass: Class<Parent>,
private val childClass: Class<Child>
) : ReadWriteProperty<ModifParent, Sequence<Child>> {
private var connectionId: ConnectionId? = null
override fun getValue(thisRef: ModifParent, property: KProperty<*>): Sequence<Child> {
if (connectionId == null) {
connectionId = ConnectionId.create(parentClass, childClass, ONE_TO_ABSTRACT_MANY, true, false)
}
return thisRef.diff.extractOneToAbstractManyChildren(connectionId!!, thisRef.id)
}
override fun setValue(thisRef: ModifParent, property: KProperty<*>, value: Sequence<Child>) {
if (!thisRef.modifiable.get()) {
throw IllegalStateException("Modifications are allowed inside 'addEntity' and 'modifyEntity' methods only!")
}
if (connectionId == null) {
connectionId = ConnectionId.create(parentClass, childClass, ONE_TO_ABSTRACT_MANY, true, false)
}
thisRef.diff.updateOneToAbstractManyChildrenOfParent(connectionId!!, thisRef.id, value)
}
}
| apache-2.0 | a6689eb187888a7fb9855f76a8ee1a30 | 49.510204 | 166 | 0.789495 | 4.881657 | false | false | false | false |
allotria/intellij-community | platform/script-debugger/protocol/protocol-model-generator/src/Generator.kt | 4 | 10657 | // Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.protocolModelGenerator
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.containers.ContainerUtil
import com.intellij.util.containers.isNullOrEmpty
import org.jetbrains.io.JsonReaderEx
import org.jetbrains.jsonProtocol.*
import org.jetbrains.protocolReader.TextOutput
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.net.URL
import java.nio.file.FileSystems
import java.nio.file.Files
import java.util.*
fun main(args: Array<String>) {
val outputDir = args[0]
val roots = IntRange(3, args.size - 1).map {
val schemaUrl = args[it]
val bytes: ByteArray
if (schemaUrl.startsWith("http")) {
bytes = loadBytes(URL(schemaUrl).openStream())
}
else {
bytes = Files.readAllBytes(FileSystems.getDefault().getPath(schemaUrl))
}
val reader = JsonReaderEx(bytes.toString(Charsets.UTF_8))
reader.isLenient = true
ProtocolSchemaReaderImpl().parseRoot(reader)
}
val mergedRoot = if (roots.size == 1) roots[0] else object : ProtocolMetaModel.Root {
override val version: ProtocolMetaModel.Version?
get() = roots[0].version
override fun domains(): List<ProtocolMetaModel.Domain> {
return ContainerUtil.concat(roots.map { it.domains() })
}
}
Generator(outputDir, args[1], args[2], mergedRoot)
}
private fun loadBytes(stream: InputStream): ByteArray {
val buffer = ByteArrayOutputStream(Math.max(stream.available(), 16 * 1024))
val bytes = ByteArray(1024 * 20)
while (true) {
val n = stream.read(bytes, 0, bytes.size)
if (n <= 0) {
break
}
buffer.write(bytes, 0, n)
}
buffer.close()
return buffer.toByteArray()
}
internal class Naming(val inputPackage: String, val requestClassName: String) {
val params = ClassNameScheme.Output("", inputPackage)
val additionalParam = ClassNameScheme.Output("", inputPackage)
val outputTypedef: ClassNameScheme = ClassNameScheme.Output("Typedef", inputPackage)
val commandResult = ClassNameScheme.Input("Result", inputPackage)
val eventData = ClassNameScheme.Input("EventData", inputPackage)
val inputValue = ClassNameScheme.Input("Value", inputPackage)
val inputEnum = ClassNameScheme.Input("", inputPackage)
val inputTypedef = ClassNameScheme.Input("Typedef", inputPackage)
val commonTypedef = ClassNameScheme.Common("Typedef", inputPackage)
}
/**
* Read metamodel and generates set of files with Java classes/interfaces for the protocol.
*/
internal class Generator(outputDir: String, rootPackage: String, requestClassName: String, metamodel: ProtocolMetaModel.Root) {
val jsonProtocolParserClassNames = ArrayList<String>()
val parserRootInterfaceItems = ArrayList<ParserRootInterfaceItem>()
val typeMap = TypeMap()
val nestedTypeMap = HashMap<NamePath, StandaloneType>()
private val fileSet = FileSet(FileSystems.getDefault().getPath(outputDir))
val naming = Naming(rootPackage, requestClassName)
init {
val domainList = metamodel.domains()
val domainGeneratorMap = HashMap<String, DomainGenerator>()
for (domain in domainList) {
if (!INCLUDED_DOMAINS.contains(domain.domain())) {
System.out.println("Domain skipped: ${domain.domain()}")
continue
}
val domainName = StringUtil.nullize(domain.domain())
val filePath = if (domainName != null) "${domainName.toLowerCase()}/$domainName.kt" else "protocol.kt"
val fileUpdater = fileSet.createFileUpdater(filePath)
val out = fileUpdater.out
out.append("// Generated source").newLine().append("package ").append(getPackageName(rootPackage, domain.domain())).newLine().newLine()
out.append("import org.jetbrains.jsonProtocol.*").newLine()
out.append("import org.jetbrains.io.JsonReaderEx").newLine()
val domainGenerator = DomainGenerator(this, domain, fileUpdater)
domainGeneratorMap.put(domain.domain(), domainGenerator)
domainGenerator.registerTypes()
out.newLine()
System.out.println("Domain generated: ${domain.domain()}")
}
typeMap.domainGeneratorMap = domainGeneratorMap
for (domainGenerator in domainGeneratorMap.values) {
domainGenerator.generateCommandsAndEvents()
}
val sharedFileUpdater = if (domainGeneratorMap.size == 1) {
domainGeneratorMap.values.first().fileUpdater
}
else {
val fileUpdater = fileSet.createFileUpdater("protocol.kt")
val out = fileUpdater.out
out.append("// Generated source").newLine().append("package ").append(rootPackage).newLine().newLine()
out.append("import org.jetbrains.jsonProtocol.*").newLine()
out.append("import org.jetbrains.io.JsonReaderEx").newLine()
fileUpdater
}
typeMap.generateRequestedTypes()
generateParserInterfaceList(sharedFileUpdater.out)
generateParserRoot(parserRootInterfaceItems, sharedFileUpdater.out)
fileSet.deleteOtherFiles()
for (domainGenerator in domainGeneratorMap.values) {
domainGenerator.fileUpdater.update()
}
if (domainGeneratorMap.size != 1) {
sharedFileUpdater.update()
}
}
fun resolveType(itemDescriptor: ItemDescriptor, scope: ResolveAndGenerateScope): TypeDescriptor {
return switchByType(itemDescriptor, object : TypeVisitor<TypeDescriptor> {
override fun visitRef(refName: String) = TypeDescriptor(resolveRefType(scope.getDomainName(), refName, scope.getTypeDirection()), itemDescriptor)
override fun visitBoolean() = TypeDescriptor(BoxableType.BOOLEAN, itemDescriptor)
override fun visitEnum(enumConstants: List<String>): TypeDescriptor {
assert(scope is MemberScope)
return TypeDescriptor((scope as MemberScope).generateEnum(itemDescriptor.description, enumConstants), itemDescriptor)
}
override fun visitString() = TypeDescriptor(BoxableType.STRING, itemDescriptor)
override fun visitInteger() = TypeDescriptor(BoxableType.INT, itemDescriptor)
override fun visitNumber() = TypeDescriptor(BoxableType.NUMBER, itemDescriptor)
override fun visitMap() = TypeDescriptor(BoxableType.MAP, itemDescriptor)
override fun visitArray(items: ProtocolMetaModel.ArrayItemType): TypeDescriptor {
val type = scope.resolveType(items).type
return TypeDescriptor(ListType(type), itemDescriptor, type == BoxableType.ANY_STRING)
}
override fun visitObject(properties: List<ProtocolMetaModel.ObjectProperty>?) = TypeDescriptor(scope.generateNestedObject(itemDescriptor.description, properties), itemDescriptor)
override fun visitUnknown() = TypeDescriptor(BoxableType.STRING, itemDescriptor, true)
})
}
private fun generateParserInterfaceList(out: TextOutput) {
// write classes in stable order
Collections.sort(jsonProtocolParserClassNames)
out.newLine().newLine().append("val PARSER_CLASSES = arrayOf(").newLine()
for (name in jsonProtocolParserClassNames) {
out.append(" ").append(name).append("::class.java")
if (name != jsonProtocolParserClassNames.last()) {
out.append(',')
}
out.newLine()
}
out.append(')')
}
private fun generateParserRoot(parserRootInterfaceItems: MutableList<ParserRootInterfaceItem>, out: TextOutput) {
// write classes in stable order
parserRootInterfaceItems.sort()
out.newLine().newLine().append("interface ").append(READER_INTERFACE_NAME).append(" : org.jetbrains.jsonProtocol.ResponseResultReader").openBlock()
for (item in parserRootInterfaceItems) {
item.writeCode(out)
out.newLine()
}
out.append("override fun readResult(methodName: String, reader: org.jetbrains.io.JsonReaderEx): Any? = ")
out.append("when (methodName)").block {
for (item in parserRootInterfaceItems) {
out.append('"')
if (!item.domain.isEmpty()) {
out.append(item.domain).append('.')
}
out.append(item.name).append('"').append(" -> ")
item.appendReadMethodName(out)
out.append("(reader)").newLine()
}
out.append("else -> null")
}
out.closeBlock()
}
/**
* Resolve absolute (DOMAIN.TYPE) or relative (TYPE) type name
*/
private fun resolveRefType(scopeDomainName: String, refName: String, direction: TypeData.Direction): BoxableType {
val pos = refName.indexOf('.')
val domainName: String
val shortName: String
if (pos == -1) {
domainName = scopeDomainName
shortName = refName
}
else {
domainName = refName.substring(0, pos)
shortName = refName.substring(pos + 1)
}
return typeMap.resolve(domainName, shortName, direction)!!
}
}
const val READER_INTERFACE_NAME: String = "ProtocolResponseReader"
private val INCLUDED_DOMAINS = arrayOf("Mono", "CSS", "Debugger", "DOM", "Inspector", "Log", "Network", "Page", "Runtime", "ServiceWorker",
"Tracing", "Target", "Overlay", "Console", "DOMDebugger", "Profiler", "HeapProfiler", "NodeWorker")
fun generateMethodNameSubstitute(originalName: String, out: TextOutput): String {
if (originalName != "this") {
return originalName
}
out.append("@org.jetbrains.jsonProtocol.ProtocolName(\"").append(originalName).append("\")").newLine()
return "get${Character.toUpperCase(originalName.get(0))}${originalName.substring(1)}"
}
fun capitalizeFirstChar(s: String): String {
if (!s.isEmpty() && s.get(0).isLowerCase()) {
return s.get(0).toUpperCase() + s.substring(1)
}
return s
}
fun <R> switchByType(typedObject: ItemDescriptor, visitor: TypeVisitor<R>): R {
val refName = if (typedObject is ItemDescriptor.Referenceable) typedObject.ref else null
if (refName != null) {
return visitor.visitRef(refName)
}
val typeName = typedObject.type
return when (typeName) {
BOOLEAN_TYPE -> visitor.visitBoolean()
STRING_TYPE, BINARY_TYPE -> if (typedObject.enum == null) visitor.visitString() else visitor.visitEnum(typedObject.enum!!)
INTEGER_TYPE, "int" -> visitor.visitInteger()
NUMBER_TYPE -> visitor.visitNumber()
ARRAY_TYPE -> visitor.visitArray(typedObject.items!!)
OBJECT_TYPE -> {
if (typedObject !is ItemDescriptor.Type) {
visitor.visitObject(null)
}
else {
val properties = typedObject.properties
return if (properties.isNullOrEmpty()) visitor.visitMap() else visitor.visitObject(properties)
}
}
ANY_TYPE, UNKNOWN_TYPE -> return visitor.visitUnknown()
else -> throw RuntimeException("Unrecognized type $typeName")
}
} | apache-2.0 | 3b091e3dad05c29f44f7c1959dd7895b | 37.476534 | 184 | 0.709862 | 4.378389 | false | false | false | false |
code-helix/slatekit | src/lib/kotlin/slatekit-meta/src/main/kotlin/slatekit/meta/InputsJSON.kt | 1 | 2942 | package slatekit.meta
import org.json.simple.JSONObject
import org.json.simple.parser.JSONParser
import org.threeten.bp.*
import slatekit.common.*
import slatekit.common.convert.Conversions
import slatekit.common.crypto.Encryptor
import slatekit.common.values.Inputs
/**
* Used to represent a request that originate from a json file.
* This is useful for automation purposes and replaying an api action from a file source.
* @param json : The JSON object
* @param enc : The encryptor
*/
data class InputsJSON(
val rawSource: Any,
val enc: Encryptor?,
val json: JSONObject
) : Inputs {
override fun get(key: String): Any? = getInternal(key)
override fun size(): Int = json.size
override val raw: Any = json
override fun getString(key: String): String = Strings.decrypt(getInternalString(key).trim()) { it -> enc?.decrypt(it) ?: it }
override fun getBool(key: String): Boolean = Conversions.toBool(getStringRaw(key))
override fun getShort(key: String): Short = Conversions.toShort(getStringRaw(key))
override fun getInt(key: String): Int = Conversions.toInt(getStringRaw(key))
override fun getLong(key: String): Long = Conversions.toLong(getStringRaw(key))
override fun getFloat(key: String): Float = Conversions.toFloat(getStringRaw(key))
override fun getDouble(key: String): Double = Conversions.toDouble(getStringRaw(key))
override fun getInstant(key: String): Instant = Conversions.toInstant(getStringRaw(key))
override fun getDateTime(key: String): DateTime = Conversions.toDateTime(getStringRaw(key))
override fun getLocalDate(key: String): LocalDate = Conversions.toLocalDate(getStringRaw(key))
override fun getLocalTime(key: String): LocalTime = Conversions.toLocalTime(getStringRaw(key))
override fun getLocalDateTime(key: String): LocalDateTime = Conversions.toLocalDateTime(getStringRaw(key))
override fun getZonedDateTime(key: String): ZonedDateTime = Conversions.toZonedDateTime(getStringRaw(key))
override fun getZonedDateTimeUtc(key: String): ZonedDateTime = Conversions.toZonedDateTimeUtc(getStringRaw(key))
override fun containsKey(key: String): Boolean {
return json.containsKey(key)
}
fun getInternal(key: String): Any? {
val value = if (json.containsKey(key)) {
json.get(key)
} else {
null
}
return value
}
fun getInternalString(key: String): String {
val value = if (json.containsKey(key)) {
json.get(key) ?: ""
} else {
""
}
return value.toString()
}
fun getStringRaw(key: String): String = getInternalString(key).trim()
companion object {
fun of(text:String, enc:Encryptor? = null):InputsJSON {
val parser = JSONParser()
val json = parser.parse(text) as JSONObject
return InputsJSON(text, enc, json)
}
}
} | apache-2.0 | b573e18dce5085c38356cc15682e20b9 | 38.24 | 129 | 0.692386 | 4.326471 | false | false | false | false |
leafclick/intellij-community | plugins/github/src/org/jetbrains/plugins/github/api/GithubApiRequestExecutor.kt | 1 | 11165 | // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.github.api
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.EmptyProgressIndicator
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.util.EventDispatcher
import com.intellij.util.ThrowableConvertor
import com.intellij.util.io.HttpRequests
import com.intellij.util.io.HttpSecurityUtil
import com.intellij.util.io.RequestBuilder
import org.jetbrains.annotations.CalledInAny
import org.jetbrains.annotations.CalledInBackground
import org.jetbrains.annotations.TestOnly
import org.jetbrains.plugins.github.api.data.GithubErrorMessage
import org.jetbrains.plugins.github.exceptions.*
import org.jetbrains.plugins.github.util.GithubSettings
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.Reader
import java.net.HttpURLConnection
import java.util.*
import java.util.function.Supplier
import java.util.zip.GZIPInputStream
/**
* Executes API requests taking care of authentication, headers, proxies, timeouts, etc.
*/
sealed class GithubApiRequestExecutor {
protected val authDataChangedEventDispatcher = EventDispatcher.create(AuthDataChangeListener::class.java)
@CalledInBackground
@Throws(IOException::class, ProcessCanceledException::class)
abstract fun <T> execute(indicator: ProgressIndicator, request: GithubApiRequest<T>): T
@TestOnly
@CalledInBackground
@Throws(IOException::class, ProcessCanceledException::class)
fun <T> execute(request: GithubApiRequest<T>): T = execute(EmptyProgressIndicator(), request)
fun addListener(listener: AuthDataChangeListener, disposable: Disposable) =
authDataChangedEventDispatcher.addListener(listener, disposable)
fun addListener(disposable: Disposable, listener: () -> Unit) =
authDataChangedEventDispatcher.addListener(object : AuthDataChangeListener {
override fun authDataChanged() {
listener()
}
}, disposable)
class WithTokenAuth internal constructor(githubSettings: GithubSettings,
token: String,
private val useProxy: Boolean) : Base(githubSettings) {
@Volatile
internal var token: String = token
set(value) {
field = value
authDataChangedEventDispatcher.multicaster.authDataChanged()
}
@Throws(IOException::class, ProcessCanceledException::class)
override fun <T> execute(indicator: ProgressIndicator, request: GithubApiRequest<T>): T {
indicator.checkCanceled()
return createRequestBuilder(request)
.tuner { connection ->
request.additionalHeaders.forEach(connection::addRequestProperty)
connection.addRequestProperty(HttpSecurityUtil.AUTHORIZATION_HEADER_NAME, "${request.tokenHeaderType} $token")
}
.useProxy(useProxy)
.execute(request, indicator)
}
}
class WithBasicAuth internal constructor(githubSettings: GithubSettings,
private val login: String,
private val password: CharArray,
private val twoFactorCodeSupplier: Supplier<String?>) : Base(githubSettings) {
private var twoFactorCode: String? = null
@Throws(IOException::class, ProcessCanceledException::class)
override fun <T> execute(indicator: ProgressIndicator, request: GithubApiRequest<T>): T {
indicator.checkCanceled()
val basicHeaderValue = HttpSecurityUtil.createBasicAuthHeaderValue(login, password)
return executeWithBasicHeader(indicator, request, basicHeaderValue)
}
private fun <T> executeWithBasicHeader(indicator: ProgressIndicator, request: GithubApiRequest<T>, header: String): T {
indicator.checkCanceled()
return try {
createRequestBuilder(request)
.tuner { connection ->
request.additionalHeaders.forEach(connection::addRequestProperty)
connection.addRequestProperty(HttpSecurityUtil.AUTHORIZATION_HEADER_NAME, "Basic $header")
twoFactorCode?.let { connection.addRequestProperty(OTP_HEADER_NAME, it) }
}
.execute(request, indicator)
}
catch (e: GithubTwoFactorAuthenticationException) {
twoFactorCode = twoFactorCodeSupplier.get() ?: throw e
executeWithBasicHeader(indicator, request, header)
}
}
}
abstract class Base(private val githubSettings: GithubSettings) : GithubApiRequestExecutor() {
protected fun <T> RequestBuilder.execute(request: GithubApiRequest<T>, indicator: ProgressIndicator): T {
indicator.checkCanceled()
try {
LOG.debug("Request: ${request.url} ${request.operationName} : Connecting")
return connect {
val connection = it.connection as HttpURLConnection
if (request is GithubApiRequest.WithBody) {
LOG.debug("Request: ${connection.requestMethod} ${connection.url} with body:\n${request.body} : Connected")
request.body?.let { body -> it.write(body) }
}
else {
LOG.debug("Request: ${connection.requestMethod} ${connection.url} : Connected")
}
checkResponseCode(connection)
indicator.checkCanceled()
val result = request.extractResult(createResponse(it, indicator))
LOG.debug("Request: ${connection.requestMethod} ${connection.url} : Result extracted")
result
}
}
catch (e: GithubStatusCodeException) {
@Suppress("UNCHECKED_CAST")
if (request is GithubApiRequest.Get.Optional<*> && e.statusCode == HttpURLConnection.HTTP_NOT_FOUND) return null as T else throw e
}
catch (e: GithubConfusingException) {
if (request.operationName != null) {
val errorText = "Can't ${request.operationName}"
e.setDetails(errorText)
LOG.debug(errorText, e)
}
throw e
}
}
protected fun createRequestBuilder(request: GithubApiRequest<*>): RequestBuilder {
return when (request) {
is GithubApiRequest.Get -> HttpRequests.request(request.url)
is GithubApiRequest.Post -> HttpRequests.post(request.url, request.bodyMimeType)
is GithubApiRequest.Put -> HttpRequests.put(request.url, request.bodyMimeType)
is GithubApiRequest.Patch -> HttpRequests.patch(request.url, request.bodyMimeType)
is GithubApiRequest.Head -> HttpRequests.head(request.url)
is GithubApiRequest.Delete -> {
if (request.body == null) HttpRequests.delete(request.url) else HttpRequests.delete(request.url, request.bodyMimeType)
}
else -> throw UnsupportedOperationException("${request.javaClass} is not supported")
}
.connectTimeout(githubSettings.connectionTimeout)
.userAgent("Intellij IDEA Github Plugin")
.throwStatusCodeException(false)
.forceHttps(false)
.accept(request.acceptMimeType)
}
@Throws(IOException::class)
private fun checkResponseCode(connection: HttpURLConnection) {
if (connection.responseCode < 400) return
val statusLine = "${connection.responseCode} ${connection.responseMessage}"
val errorText = getErrorText(connection)
LOG.debug("Request: ${connection.requestMethod} ${connection.url} : Error ${statusLine} body:\n${errorText}")
val jsonError = errorText?.let { getJsonError(connection, it) }
jsonError ?: LOG.debug("Request: ${connection.requestMethod} ${connection.url} : Unable to parse JSON error")
throw when (connection.responseCode) {
HttpURLConnection.HTTP_UNAUTHORIZED,
HttpURLConnection.HTTP_PAYMENT_REQUIRED,
HttpURLConnection.HTTP_FORBIDDEN -> {
val otpHeader = connection.getHeaderField(OTP_HEADER_NAME)
if (otpHeader != null && otpHeader.contains("required", true)) {
GithubTwoFactorAuthenticationException(jsonError?.presentableError ?: errorText)
}
else if (jsonError?.containsReasonMessage("API rate limit exceeded") == true) {
GithubRateLimitExceededException(jsonError.presentableError)
}
else GithubAuthenticationException("Request response: " + (jsonError?.presentableError ?: errorText ?: statusLine))
}
else -> {
if (jsonError != null) {
GithubStatusCodeException("$statusLine - ${jsonError.presentableError}", jsonError, connection.responseCode)
}
else {
GithubStatusCodeException("$statusLine - ${errorText}", connection.responseCode)
}
}
}
}
private fun getErrorText(connection: HttpURLConnection): String? {
val errorStream = connection.errorStream ?: return null
val stream = if (connection.contentEncoding == "gzip") GZIPInputStream(errorStream) else errorStream
return InputStreamReader(stream, Charsets.UTF_8).use { it.readText() }
}
private fun getJsonError(connection: HttpURLConnection, errorText: String): GithubErrorMessage? {
if (!connection.contentType.startsWith(GithubApiContentHelper.JSON_MIME_TYPE)) return null
return try {
return GithubApiContentHelper.fromJson(errorText)
}
catch (jse: GithubJsonException) {
null
}
}
private fun createResponse(request: HttpRequests.Request, indicator: ProgressIndicator): GithubApiResponse {
return object : GithubApiResponse {
override fun findHeader(headerName: String): String? = request.connection.getHeaderField(headerName)
override fun <T> readBody(converter: ThrowableConvertor<Reader, T, IOException>): T = request.getReader(indicator).use {
converter.convert(it)
}
override fun <T> handleBody(converter: ThrowableConvertor<InputStream, T, IOException>): T = request.inputStream.use {
converter.convert(it)
}
}
}
}
class Factory {
@CalledInAny
fun create(token: String): WithTokenAuth {
return create(token, true)
}
@CalledInAny
fun create(token: String, useProxy: Boolean = true): WithTokenAuth {
return WithTokenAuth(GithubSettings.getInstance(), token, useProxy)
}
@CalledInAny
internal fun create(login: String, password: CharArray, twoFactorCodeSupplier: Supplier<String?>): WithBasicAuth {
return WithBasicAuth(GithubSettings.getInstance(), login, password, twoFactorCodeSupplier)
}
companion object {
@JvmStatic
fun getInstance(): Factory = service()
}
}
companion object {
private val LOG = logger<GithubApiRequestExecutor>()
private const val OTP_HEADER_NAME = "X-GitHub-OTP"
}
interface AuthDataChangeListener : EventListener {
fun authDataChanged()
}
enum class TokenHeaderType {
TOKEN, BEARER
}
} | apache-2.0 | 143f3f57c540c5e5d31fa82b6d358220 | 41.295455 | 140 | 0.697 | 4.97549 | false | false | false | false |
pyamsoft/padlock | padlock-base/src/main/java/com/pyamsoft/padlock/base/PadLockPreferencesImpl.kt | 1 | 7432 | /*
* Copyright 2019 Peter Kenji Yamanaka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.pyamsoft.padlock.base
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.preference.PreferenceManager
import com.pyamsoft.padlock.api.preferences.ClearPreferences
import com.pyamsoft.padlock.api.preferences.InstallListenerPreferences
import com.pyamsoft.padlock.api.preferences.LockListPreferences
import com.pyamsoft.padlock.api.preferences.LockScreenPreferences
import com.pyamsoft.padlock.api.preferences.MasterPinPreferences
import com.pyamsoft.padlock.api.preferences.PreferenceWatcher
import com.pyamsoft.padlock.api.preferences.ServicePreferences
import com.pyamsoft.padlock.model.LockScreenType
import com.pyamsoft.padlock.model.service.ServicePauseState
import com.pyamsoft.padlock.model.service.ServicePauseState.STARTED
import com.pyamsoft.padlock.model.service.ServicePauseState.valueOf
import timber.log.Timber
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
internal class PadLockPreferencesImpl @Inject internal constructor(
private val context: Context
) : MasterPinPreferences,
ClearPreferences,
InstallListenerPreferences,
LockListPreferences,
LockScreenPreferences,
ServicePreferences {
private val preferences by lazy { PreferenceManager.getDefaultSharedPreferences(context) }
private val globalPreferenceWatcher by lazy { GlobalPreferenceWatcher(preferences) }
private val ignoreTimeKey: String
private val timeoutTimeKey: String
private val installListener: String
private val lockScreenType: String
private val ignoreTimeDefault: String
private val timeoutTimeDefault: String
private val lockScreenTypeDefault: String
private val installListenerDefault: Boolean
init {
val res = context.resources
ignoreTimeKey = res.getString(R.string.ignore_time_key)
ignoreTimeDefault = res.getString(R.string.ignore_time_default)
timeoutTimeKey = res.getString(R.string.timeout_time_key)
timeoutTimeDefault = res.getString(R.string.timeout_time_default)
installListener = res.getString(R.string.install_listener_key)
installListenerDefault = res.getBoolean(R.bool.install_listener_default)
lockScreenType = res.getString(R.string.lock_screen_type_key)
lockScreenTypeDefault = res.getString(R.string.lock_screen_type_default)
}
private inline fun edit(onEdit: SharedPreferences.Editor.() -> Unit) {
preferences.edit()
.apply(onEdit)
.apply()
}
override fun getCurrentLockType(): LockScreenType =
LockScreenType.valueOf(preferences.getString(lockScreenType, lockScreenTypeDefault).orEmpty())
override fun isInstallListenerEnabled(): Boolean =
preferences.getBoolean(installListener, installListenerDefault)
override fun getHint(): String? = preferences.getString(HINT, null)
override fun setHint(hint: String) {
edit {
putString(HINT, hint)
}
}
override fun clearHint() {
edit {
remove(HINT)
}
}
override fun getDefaultIgnoreTime(): Long =
preferences.getString(ignoreTimeKey, ignoreTimeDefault).orEmpty().toLong()
override fun getTimeoutPeriod(): Long =
preferences.getString(timeoutTimeKey, timeoutTimeDefault).orEmpty().toLong()
override fun isSystemVisible(): Boolean = preferences.getBoolean(IS_SYSTEM, false)
override fun setSystemVisible(visible: Boolean) {
edit {
putBoolean(IS_SYSTEM, visible)
}
}
override fun getMasterPassword(): String? = preferences.getString(MASTER_PASSWORD, null)
override fun setMasterPassword(pw: String) {
edit {
putString(MASTER_PASSWORD, pw)
}
}
override fun clearMasterPassword() {
edit {
remove(MASTER_PASSWORD)
}
}
override fun getPaused(): ServicePauseState {
return valueOf(preferences.getString(PAUSED, STARTED.name).orEmpty())
}
override fun setPaused(paused: ServicePauseState) {
edit {
putString(PAUSED, paused.name)
}
}
override fun watchPausedState(func: (ServicePauseState) -> Unit): PreferenceWatcher {
return KeyedPreferenceWatcher(globalPreferenceWatcher, PAUSED) {
func(getPaused())
}
}
override fun watchPinPresence(func: (Boolean) -> Unit): PreferenceWatcher {
return KeyedPreferenceWatcher(globalPreferenceWatcher, MASTER_PASSWORD) {
func(!getMasterPassword().isNullOrEmpty())
}
}
override fun watchSystemVisible(func: (Boolean) -> Unit): PreferenceWatcher {
return KeyedPreferenceWatcher(globalPreferenceWatcher, IS_SYSTEM) {
func(isSystemVisible())
}
}
@SuppressLint("ApplySharedPref")
private fun clear(preferences: SharedPreferences) {
preferences.edit()
.clear()
.commit()
}
override fun clearAll() {
val defaultValuesKey = PreferenceManager.KEY_HAS_SET_DEFAULT_VALUES
val defaultPrefs = context.getSharedPreferences(defaultValuesKey, Context.MODE_PRIVATE)
clear(preferences)
clear(defaultPrefs)
}
private class GlobalPreferenceWatcher internal constructor(
private val preferences: SharedPreferences
) {
private val watcherMap = ConcurrentHashMap<UUID, Pair<String, () -> Unit>>()
private val callback = OnSharedPreferenceChangeListener { _, preference ->
val values = watcherMap.values
for (entry in values) {
val key = entry.first
val func = entry.second
if (key == preference) {
func()
}
}
}
fun addWatcher(
uuid: UUID,
key: String,
func: () -> Unit
) {
if (watcherMap.isEmpty()) {
preferences.registerOnSharedPreferenceChangeListener(callback)
}
Timber.d("Adding PreferenceWatcher for key $key")
watcherMap[uuid] = key to func
}
fun removeWatcher(uuid: UUID) {
watcherMap.remove(uuid)
?.also { (key, _) ->
Timber.d("Removing PreferenceWatcher for key $key")
}
if (watcherMap.isEmpty()) {
Timber.d("Stop watching preferences")
preferences.unregisterOnSharedPreferenceChangeListener(callback)
}
}
}
private class KeyedPreferenceWatcher(
private val globalPreferenceWatcher: GlobalPreferenceWatcher,
key: String,
func: () -> Unit
) : PreferenceWatcher {
private val uuid = UUID.randomUUID()
init {
globalPreferenceWatcher.addWatcher(uuid, key, func)
}
override fun stopWatching() {
globalPreferenceWatcher.removeWatcher(uuid)
}
}
companion object {
private const val IS_SYSTEM = "is_system"
private const val MASTER_PASSWORD = "master_password"
private const val HINT = "hint"
private const val PAUSED = "paused_state"
}
}
| apache-2.0 | 52d9b3d7fe79b663281bfcc65b517ea4 | 29.710744 | 98 | 0.733181 | 4.487923 | false | false | false | false |
smmribeiro/intellij-community | platform/vcs-impl/src/com/intellij/openapi/vcs/impl/PartialLineStatusTrackerManagerState.kt | 12 | 6581 | // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.vcs.impl
import com.intellij.diff.util.Range
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.registry.Registry
import com.intellij.openapi.vcs.changes.ChangeListManager
import com.intellij.openapi.vcs.ex.ChangelistsLocalLineStatusTracker
import com.intellij.openapi.vcs.ex.ChangelistsLocalLineStatusTracker.RangeState
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.xml.util.XmlStringUtil
import org.jdom.Element
import org.jetbrains.annotations.NonNls
private typealias TrackerState = ChangelistsLocalLineStatusTracker.State
private typealias FullTrackerState = ChangelistsLocalLineStatusTracker.FullState
@Service
@State(name = "LineStatusTrackerManager", storages = [(Storage(value = StoragePathMacros.WORKSPACE_FILE))])
@NonNls
class PartialLineStatusTrackerManagerState(private val project: Project) : PersistentStateComponent<Element> {
private var storedFileStates: List<TrackerState> = listOf()
private var wasLoaded: Boolean = false
override fun getState(): Element {
val element = Element("state")
val areChangeListsEnabled = ChangeListManager.getInstance(project).areChangeListsEnabled()
val fileStates = when {
areChangeListsEnabled -> LineStatusTrackerManager.getInstanceImpl(project).collectPartiallyChangedFilesStates()
storedFileStates.size < DISABLED_FILES_THRESHOLD -> storedFileStates
else -> emptyList()
}
for (state in fileStates) {
element.addContent(writePartialFileState(state))
}
return element
}
override fun loadState(element: Element) {
if (wasLoaded) return
storedFileStates = parseStates(element)
}
private fun getStatesAndClear(): List<TrackerState> {
val result = storedFileStates
storedFileStates = emptyList()
wasLoaded = true
return result
}
companion object {
private const val DISABLED_FILES_THRESHOLD = 20
private const val NODE_PARTIAL_FILE = "file"
private const val ATT_PATH = "path"
private const val NODE_VCS = "vcs"
private const val NODE_CURRENT = "current"
private const val ATT_CONTENT = "content"
private const val NODE_RANGES = "ranges"
private const val NODE_RANGE = "range"
private const val ATT_START_1 = "start1"
private const val ATT_END_1 = "end1"
private const val ATT_START_2 = "start2"
private const val ATT_END_2 = "end2"
private const val ATT_CHANGELIST_ID = "changelist"
@JvmStatic
internal fun restoreState(project: Project) {
if (!ChangeListManager.getInstance(project).areChangeListsEnabled()) return
val fileStates = project.service<PartialLineStatusTrackerManagerState>().getStatesAndClear()
if (fileStates.isEmpty()) return
ChangeListManager.getInstance(project).invokeAfterUpdate(true) {
LineStatusTrackerManager.getInstanceImpl(project).restoreTrackersForPartiallyChangedFiles(fileStates)
}
}
@JvmStatic
internal fun saveCurrentState(project: Project, fileStates: List<TrackerState>) {
val stateService = project.service<PartialLineStatusTrackerManagerState>()
stateService.storedFileStates = fileStates
}
private fun parseStates(element: Element): List<TrackerState> {
val fileStates = mutableListOf<TrackerState>()
for (node in element.getChildren(NODE_PARTIAL_FILE)) {
val state = readPartialFileState(node)
if (state != null) fileStates.add(state)
}
return fileStates
}
private fun writePartialFileState(state: TrackerState): Element {
val element = Element(NODE_PARTIAL_FILE)
element.setAttribute(ATT_PATH, state.virtualFile.path)
if (state is FullTrackerState && Registry.`is`("vcs.enable.partial.changelists.persist.file.contents")) {
// TODO: should not be stored in workspace.xml; Project.getProjectCachePath ?
element.addContent(Element(NODE_VCS).setAttribute(ATT_CONTENT, XmlStringUtil.escapeIllegalXmlChars(state.vcsContent)))
element.addContent(Element(NODE_CURRENT).setAttribute(ATT_CONTENT, XmlStringUtil.escapeIllegalXmlChars(state.currentContent)))
}
val rangesNode = Element(NODE_RANGES)
for (it in state.ranges) {
rangesNode.addContent(writeRangeState(it))
}
element.addContent(rangesNode)
return element
}
private fun readPartialFileState(element: Element): TrackerState? {
val path = element.getAttributeValue(ATT_PATH) ?: return null
val virtualFile = LocalFileSystem.getInstance().findFileByPath(path) ?: return null
val vcsContent = element.getChild(NODE_VCS)?.getAttributeValue(ATT_CONTENT)
val currentContent = element.getChild(NODE_CURRENT)?.getAttributeValue(ATT_CONTENT)
val rangeStates = mutableListOf<RangeState>()
val rangesNode = element.getChild(NODE_RANGES) ?: return null
for (node in rangesNode.getChildren(NODE_RANGE)) {
val rangeState = readRangeState(node) ?: return null
rangeStates.add(rangeState)
}
if (vcsContent != null && currentContent != null) {
return FullTrackerState(virtualFile, rangeStates,
XmlStringUtil.unescapeIllegalXmlChars(vcsContent),
XmlStringUtil.unescapeIllegalXmlChars(currentContent))
}
else {
return TrackerState(virtualFile, rangeStates)
}
}
private fun writeRangeState(range: RangeState): Element {
return Element(NODE_RANGE)
.setAttribute(ATT_START_1, range.range.start1.toString())
.setAttribute(ATT_END_1, range.range.end1.toString())
.setAttribute(ATT_START_2, range.range.start2.toString())
.setAttribute(ATT_END_2, range.range.end2.toString())
.setAttribute(ATT_CHANGELIST_ID, range.changelistId)
}
private fun readRangeState(node: Element): RangeState? {
val start1 = node.getAttributeValue(ATT_START_1)?.toIntOrNull() ?: return null
val end1 = node.getAttributeValue(ATT_END_1)?.toIntOrNull() ?: return null
val start2 = node.getAttributeValue(ATT_START_2)?.toIntOrNull() ?: return null
val end2 = node.getAttributeValue(ATT_END_2)?.toIntOrNull() ?: return null
val changelistId = node.getAttributeValue(ATT_CHANGELIST_ID) ?: return null
return RangeState(Range(start1, end1, start2, end2), changelistId)
}
}
} | apache-2.0 | 9ede7e9b04b7b717079253d369a679e9 | 39.881988 | 140 | 0.726333 | 4.641044 | false | false | false | false |
smmribeiro/intellij-community | plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/usages/KotlinWrapperForJavaUsageInfos.kt | 6 | 923 | // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.refactoring.changeSignature.usages
import com.intellij.psi.PsiElement
import com.intellij.refactoring.changeSignature.JavaChangeInfo
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinChangeInfo
class KotlinWrapperForJavaUsageInfos(
val kotlinChangeInfo: KotlinChangeInfo,
val javaChangeInfo: JavaChangeInfo,
val javaUsageInfos: Array<UsageInfo>,
primaryMethod: PsiElement
) : UsageInfo(primaryMethod) {
override fun hashCode() = javaChangeInfo.method.hashCode()
override fun equals(other: Any?): Boolean {
return other === this || (other is KotlinWrapperForJavaUsageInfos && javaChangeInfo.method == other.javaChangeInfo.method)
}
}
| apache-2.0 | 4548060ecf943876fdbb0a392cf0be8c | 42.952381 | 158 | 0.789816 | 4.661616 | false | false | false | false |
smmribeiro/intellij-community | plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/FoldIfToFunctionCallIntention.kt | 1 | 6774 | // Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.intentions.*
import org.jetbrains.kotlin.idea.util.reformatted
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedExpressionForSelectorOrThis
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class FoldIfToFunctionCallIntention : SelfTargetingRangeIntention<KtIfExpression>(
KtIfExpression::class.java,
KotlinBundle.lazyMessage("lift.function.call.out.of.if"),
) {
override fun applicabilityRange(element: KtIfExpression): TextRange? =
if (canFoldToFunctionCall(element)) element.ifKeyword.textRange else null
override fun applyTo(element: KtIfExpression, editor: Editor?) {
foldToFunctionCall(element)
}
companion object {
fun branches(expression: KtExpression): List<KtExpression>? {
val branches = when (expression) {
is KtIfExpression -> expression.branches
is KtWhenExpression -> expression.entries.map { it.expression }
else -> emptyList()
}
val branchesSize = branches.size
if (branchesSize < 2) return null
return branches.filterNotNull().takeIf { it.size == branchesSize }
}
private fun canFoldToFunctionCall(element: KtExpression): Boolean {
val branches = branches(element) ?: return false
val callExpressions = branches.mapNotNull { it.callExpression() }
if (branches.size != callExpressions.size) return false
if (differentArgumentIndex(callExpressions) == null) return false
val headCall = callExpressions.first()
val tailCalls = callExpressions.drop(1)
val context = headCall.analyze(BodyResolveMode.PARTIAL)
val (headFunctionFqName, headFunctionParameters) = headCall.fqNameAndParameters(context) ?: return false
return tailCalls.all { call ->
val (fqName, parameters) = call.fqNameAndParameters(context) ?: return@all false
fqName == headFunctionFqName && parameters.zip(headFunctionParameters).all { it.first == it.second }
}
}
private fun foldToFunctionCall(element: KtExpression) {
val branches = branches(element) ?: return
val callExpressions = branches.mapNotNull { it.callExpression() }
val headCall = callExpressions.first()
val argumentIndex = differentArgumentIndex(callExpressions) ?: return
val hasNamedArgument = callExpressions.any { call -> call.valueArguments.any { it.getArgumentName() != null } }
val copiedIf = element.copy() as KtIfExpression
copiedIf.branches.forEach { branch ->
val call = branch.callExpression() ?: return
val argument = call.valueArguments[argumentIndex].getArgumentExpression() ?: return
call.getQualifiedExpressionForSelectorOrThis().replace(argument)
}
headCall.valueArguments[argumentIndex].getArgumentExpression()?.replace(copiedIf)
if (hasNamedArgument) {
headCall.valueArguments.forEach {
if (it.getArgumentName() == null) AddNameToArgumentIntention.apply(it)
}
}
element.replace(headCall.getQualifiedExpressionForSelectorOrThis()).reformatted()
}
private fun differentArgumentIndex(callExpressions: List<KtCallExpression>): Int? {
val headCall = callExpressions.first()
val headCalleeText = headCall.calleeText()
val tailCalls = callExpressions.drop(1)
if (headCall.valueArguments.any { it is KtLambdaArgument }) return null
val headArguments = headCall.valueArguments.mapNotNull { it.getArgumentExpression()?.text }
val headArgumentsSize = headArguments.size
if (headArgumentsSize != headCall.valueArguments.size) return null
val differentArgumentIndexes = tailCalls.mapNotNull { call ->
if (call.calleeText() != headCalleeText) return@mapNotNull null
val arguments = call.valueArguments.mapNotNull { it.getArgumentExpression()?.text }
if (arguments.size != headArgumentsSize) return@mapNotNull null
val differentArgumentIndexes = arguments.zip(headArguments).mapIndexedNotNull { index, (arg, headArg) ->
if (arg != headArg) index else null
}
differentArgumentIndexes.singleOrNull()
}
if (differentArgumentIndexes.size != tailCalls.size || differentArgumentIndexes.distinct().size != 1) return null
return differentArgumentIndexes.first()
}
private fun KtExpression?.callExpression(): KtCallExpression? {
return when (val expression = if (this is KtBlockExpression) statements.singleOrNull() else this) {
is KtCallExpression -> expression
is KtQualifiedExpression -> expression.callExpression
else -> null
}?.takeIf { it.calleeExpression != null }
}
private fun KtCallExpression.calleeText(): String {
val parent = this.parent
val (receiver, op) = if (parent is KtQualifiedExpression) {
parent.receiverExpression.text to parent.operationSign.value
} else {
"" to ""
}
return "$receiver$op${calleeExpression?.text.orEmpty()}"
}
private fun KtCallExpression.fqNameAndParameters(context: BindingContext): Pair<FqName, List<ValueParameterDescriptor>>? {
val resolvedCall = getResolvedCall(context) ?: return null
val fqName = resolvedCall.resultingDescriptor.fqNameOrNull() ?: return null
val parameters = valueArguments.mapNotNull { (resolvedCall.getArgumentMapping(it) as? ArgumentMatch)?.valueParameter }
return fqName to parameters
}
}
}
| apache-2.0 | e7867c57c677f0ec213e0080de3bb10c | 50.318182 | 140 | 0.672719 | 5.543372 | false | false | false | false |
cout970/Modeler | src/main/kotlin/com/cout970/modeler/gui/rcomponents/left/Left.kt | 1 | 4906 | package com.cout970.modeler.gui.rcomponents.left
import com.cout970.modeler.core.project.IProgramState
import com.cout970.modeler.gui.GuiState
import com.cout970.modeler.gui.canvas.CanvasContainer
import com.cout970.modeler.gui.canvas.GridLines
import com.cout970.modeler.gui.leguicomp.classes
import com.cout970.modeler.gui.leguicomp.clear
import com.cout970.modeler.render.tool.Animator
import com.cout970.reactive.core.*
import com.cout970.reactive.dsl.*
import com.cout970.reactive.nodes.*
import org.liquidengine.legui.component.Button
import org.liquidengine.legui.component.Component
import org.liquidengine.legui.component.TextInput
import org.liquidengine.legui.component.misc.listener.scrollablepanel.ScrollablePanelViewportScrollListener
import org.liquidengine.legui.component.optional.align.HorizontalAlign
import org.liquidengine.legui.event.ScrollEvent
import org.liquidengine.legui.input.Mouse
import org.liquidengine.legui.system.handler.SehUtil
data class LeftPanelProps(
val visible: Boolean, val programState: GuiState,
val grids: GridLines, val animator: Animator,
val canvasContainer: CanvasContainer,
val reRender: () -> Unit
) : RProps
class LeftPanel : RStatelessComponent<LeftPanelProps>() {
override fun RBuilder.render() = div("LeftPanel") {
style {
posX = 0f
posY = 48f
classes(if (!props.visible) "left_panel_hide" else "left_panel")
}
postMount {
width = 288f
height = parent.size.y - 48f
}
scrollablePanel {
style {
borderless()
transparent()
}
postMount {
posX = 0f
posY = 5f
sizeX = parent.sizeX
sizeY = parent.sizeY - posY + 8f
}
horizontalScroll { style { hide() } }
verticalScroll {
style {
isArrowsEnabled = false
classes("vertical_scroll", "left_panel_scroll")
}
}
viewport {
style {
transparent()
borderless()
}
postMount {
listenerMap.clear(ScrollEvent::class.java)
listenerMap.addListener(ScrollEvent::class.java, CustomScrollablePanelViewportScrollListener())
}
}
container {
style {
borderless()
transparent()
width = 288f - 8f
height = 1200f
}
postMount {
floatTop(6f)
height = childComponents.last().let { it.posY + it.sizeY }
}
child(EditObjectPanel::class, ModelAccessorProps(props.programState))
child(EditGroupPanel::class, ModelAccessorProps(props.programState))
child(EditKeyframe::class, EditKeyframeProps(props.animator, props.programState))
child(EditChannel::class, EditChannelProps(props.animator, props.programState))
child(EditorControls::class, EditorControlsProps(props.programState))
child(EditGrids::class, EditGridsProps(props.grids))
child(EditCanvas::class, EditCanvasProps(props.canvasContainer, props.reRender))
}
}
}
}
data class VisibleWidget(val on: Boolean) : RState
data class ModelAccessorProps(val access: IProgramState) : RProps
data class GroupTitleProps(val title: String, val on: Boolean, val toggle: () -> Unit) : RProps
class GroupTitle : RStatelessComponent<GroupTitleProps>() {
override fun RBuilder.render() {
comp(Button()) {
style {
textState.apply {
this.text = props.title
horizontalAlign = HorizontalAlign.CENTER
fontSize = 24f
}
classes(if (props.on) "group_title_pressed" else "group_title")
}
postMount {
posX = 0f
posY = 0f
sizeX = parent.sizeX
sizeY = 24f
}
onRelease {
props.toggle()
}
}
}
}
class CustomScrollablePanelViewportScrollListener : ScrollablePanelViewportScrollListener() {
override fun process(event: ScrollEvent<*>) {
val targetList = mutableListOf<Component>()
SehUtil.recursiveTargetComponentListSearch(Mouse.getCursorPosition(), event.targetComponent, targetList)
for (component in targetList) {
if (component is TextInput || component.metadata[Renderer.METADATA_KEY] == "TinyFloatInput") {
return
}
}
super.process(event)
}
} | gpl-3.0 | 6bdc6ebcec427115cdd5b0cb1d2b6130 | 32.155405 | 115 | 0.589075 | 4.838264 | false | false | false | false |
PhilippeBoisney/Material | app/src/main/java/io/philippeboisney/material/animation/FabAnimatableTransition.kt | 1 | 1528 | package io.philippeboisney.material.animation
import android.animation.*
import android.content.Context
import android.util.AttributeSet
import android.annotation.TargetApi
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
import com.google.android.material.floatingactionbutton.FloatingActionButton
import io.philippeboisney.material.R
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
class FabAnimatableTransition(context: Context, attrs: AttributeSet) : BaseForcedTransition(context, attrs) {
private val animatable: Animatable
init {
val a = context.obtainStyledAttributes(attrs, R.styleable.FabAnimatable)
val drawable = a.getDrawable(R.styleable.FabAnimatable_android_src)
a.recycle()
if (drawable is Animatable) {
animatable = drawable
} else {
throw IllegalArgumentException("Non-Animatable resource provided.")
}
}
override fun getAnimator(view: View): Animator
= createAnimation(view)
// ---
private fun createAnimation(view: View): ValueAnimator {
val fab = view as FloatingActionButton
fab.setImageDrawable(animatable as Drawable)
val transition = ValueAnimator.ofInt(0, 1)
transition.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
animatable.start()
}
})
return transition!!
}
} | apache-2.0 | f3db798839c9922a02883e6fe75f268a | 30.854167 | 109 | 0.712042 | 4.866242 | false | false | false | false |
leafclick/intellij-community | python/src/com/jetbrains/python/inspections/PyNamedTupleInspection.kt | 1 | 6577 | // Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.jetbrains.python.inspections
import com.intellij.codeInspection.LocalInspectionToolSession
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.ResolveState
import com.intellij.psi.scope.PsiScopeProcessor
import com.jetbrains.python.codeInsight.stdlib.PyNamedTupleTypeProvider
import com.jetbrains.python.psi.LanguageLevel
import com.jetbrains.python.psi.PyClass
import com.jetbrains.python.psi.PyTargetExpression
import com.jetbrains.python.psi.types.TypeEvalContext
import java.util.*
class PyNamedTupleInspection : PyInspection() {
companion object {
fun inspectFieldsOrder(cls: PyClass,
classFieldsFilter: (PyClass) -> Boolean,
checkInheritedOrder: Boolean,
context: TypeEvalContext,
callback: (PsiElement, String, ProblemHighlightType) -> Unit,
fieldsFilter: (PyTargetExpression) -> Boolean = { true },
hasAssignedValue: (PyTargetExpression) -> Boolean = PyTargetExpression::hasAssignedValue) {
val fieldsProcessor = if (classFieldsFilter(cls)) processFields(cls, fieldsFilter, hasAssignedValue) else null
if (fieldsProcessor?.lastFieldWithoutDefaultValue == null && !checkInheritedOrder) return
val ancestors = cls.getAncestorClasses(context)
val ancestorsFields = ancestors.map {
if (!classFieldsFilter(it)) {
Ancestor.FILTERED
}
else {
val processor = processFields(it, fieldsFilter, hasAssignedValue)
if (processor.fieldsWithDefaultValue.isNotEmpty()) {
Ancestor.HAS_FIELD_WITH_DEFAULT_VALUE
}
else if (processor.lastFieldWithoutDefaultValue != null) {
Ancestor.HAS_NOT_FIELD_WITH_DEFAULT_VALUE
}
else {
Ancestor.FILTERED
}
}
}
if (checkInheritedOrder) {
var seenAncestorHavingFieldWithDefaultValue: PyClass? = null
for (ancestorAndFields in ancestors.zip(ancestorsFields).asReversed()) {
if (ancestorAndFields.second == Ancestor.HAS_FIELD_WITH_DEFAULT_VALUE) seenAncestorHavingFieldWithDefaultValue = ancestorAndFields.first
else if (ancestorAndFields.second == Ancestor.HAS_NOT_FIELD_WITH_DEFAULT_VALUE && seenAncestorHavingFieldWithDefaultValue != null) {
callback(
cls.superClassExpressionList!!,
"Inherited non-default argument(s) defined in ${ancestorAndFields.first.name} follows " +
"inherited default argument defined in ${seenAncestorHavingFieldWithDefaultValue.name}",
ProblemHighlightType.GENERIC_ERROR
)
break
}
}
}
val lastFieldWithoutDefaultValue = fieldsProcessor?.lastFieldWithoutDefaultValue
if (lastFieldWithoutDefaultValue != null) {
if (ancestorsFields.contains(Ancestor.HAS_FIELD_WITH_DEFAULT_VALUE)) {
cls.nameIdentifier?.let { name ->
val ancestorsNames = ancestors
.asSequence()
.zip(ancestorsFields.asSequence())
.filter { it.second == Ancestor.HAS_FIELD_WITH_DEFAULT_VALUE }
.joinToString { "'${it.first.name}'" }
callback(name,
"Non-default argument(s) follows default argument(s) defined in $ancestorsNames",
ProblemHighlightType.GENERIC_ERROR)
}
}
fieldsProcessor.fieldsWithDefaultValue.headSet(lastFieldWithoutDefaultValue).forEach {
callback(it,
"Fields with a default value must come after any fields without a default.",
ProblemHighlightType.GENERIC_ERROR)
}
}
}
private fun processFields(cls: PyClass,
filter: (PyTargetExpression) -> Boolean,
hasAssignedValue: (PyTargetExpression) -> Boolean): LocalFieldsProcessor {
val fieldsProcessor = LocalFieldsProcessor(filter, hasAssignedValue)
cls.processClassLevelDeclarations(fieldsProcessor)
return fieldsProcessor
}
private enum class Ancestor {
FILTERED, HAS_FIELD_WITH_DEFAULT_VALUE, HAS_NOT_FIELD_WITH_DEFAULT_VALUE
}
}
override fun buildVisitor(holder: ProblemsHolder,
isOnTheFly: Boolean,
session: LocalInspectionToolSession): PsiElementVisitor = Visitor(holder, session)
private class Visitor(holder: ProblemsHolder, session: LocalInspectionToolSession) : PyInspectionVisitor(holder, session) {
override fun visitPyClass(node: PyClass?) {
super.visitPyClass(node)
if (node != null &&
LanguageLevel.forElement(node).isAtLeast(LanguageLevel.PYTHON36) &&
PyNamedTupleTypeProvider.isTypingNamedTupleDirectInheritor(node, myTypeEvalContext)) {
inspectFieldsOrder(node, { it == node }, false, myTypeEvalContext, this::registerProblem)
}
}
}
private class LocalFieldsProcessor(private val filter: (PyTargetExpression) -> Boolean,
private val hasAssignedValue: (PyTargetExpression) -> Boolean) : PsiScopeProcessor {
val lastFieldWithoutDefaultValue: PyTargetExpression?
get() = lastFieldWithoutDefaultValueBox.result
val fieldsWithDefaultValue: TreeSet<PyTargetExpression>
private val lastFieldWithoutDefaultValueBox: MaxBy<PyTargetExpression>
init {
val offsetComparator = compareBy(PyTargetExpression::getTextOffset)
lastFieldWithoutDefaultValueBox = MaxBy(offsetComparator)
fieldsWithDefaultValue = TreeSet(offsetComparator)
}
override fun execute(element: PsiElement, state: ResolveState): Boolean {
if (element is PyTargetExpression && filter(element)) {
when {
hasAssignedValue(element) -> fieldsWithDefaultValue.add(element)
else -> lastFieldWithoutDefaultValueBox.apply(element)
}
}
return true
}
}
private class MaxBy<T>(private val comparator: Comparator<T>) {
var result: T? = null
private set
fun apply(t: T) {
if (result == null || comparator.compare(result, t) < 0) {
result = t
}
}
}
} | apache-2.0 | 41400807f7151e3de9e0d1ed0468ff87 | 39.857143 | 146 | 0.668238 | 5.154389 | false | false | false | false |
siosio/intellij-community | plugins/kotlin/jvm-debugger/core/src/org/jetbrains/kotlin/idea/debugger/stepping/filter/KotlinStepOverFilter.kt | 1 | 3016 | // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.debugger.stepping.filter
import com.intellij.debugger.engine.DebugProcess.JAVA_STRATUM
import com.intellij.debugger.engine.DebugProcessImpl
import com.intellij.debugger.engine.SuspendContextImpl
import com.intellij.openapi.project.Project
import com.intellij.util.Range
import com.sun.jdi.Location
import com.sun.jdi.StackFrame
import org.jetbrains.kotlin.codegen.inline.isFakeLocalVariableForInline
import org.jetbrains.kotlin.idea.debugger.safeLineNumber
import org.jetbrains.kotlin.idea.debugger.safeMethod
import org.jetbrains.kotlin.idea.debugger.safeVariables
import org.jetbrains.kotlin.idea.debugger.stepping.KotlinMethodFilter
@Suppress("EqualsOrHashCode")
data class StepOverCallerInfo(val declaringType: String, val methodName: String?, val methodSignature: String?) {
companion object {
fun from(location: Location): StepOverCallerInfo {
val method = location.safeMethod()
val declaringType = location.declaringType().name()
val methodName = method?.name()
val methodSignature = method?.signature()
return StepOverCallerInfo(declaringType, methodName, methodSignature)
}
}
}
data class LocationToken(val lineNumber: Int, val inlineVariables: List<String>) {
companion object {
fun from(stackFrame: StackFrame): LocationToken {
val location = stackFrame.location()
val lineNumber = location.safeLineNumber(JAVA_STRATUM)
val methodVariables = ArrayList<String>(0)
for (variable in location.safeMethod()?.safeVariables() ?: emptyList()) {
val name = variable.name()
if (variable.isVisible(stackFrame) && isFakeLocalVariableForInline(name)) {
methodVariables += name
}
}
return LocationToken(lineNumber, methodVariables)
}
}
}
class KotlinStepOverFilter(
val project: Project,
private val tokensToSkip: Set<LocationToken>,
private val callerInfo: StepOverCallerInfo
) : KotlinMethodFilter {
override fun locationMatches(context: SuspendContextImpl, location: Location): Boolean {
val stackFrame = context.frameProxy?.stackFrame ?: return true
val token = LocationToken.from(stackFrame)
val callerInfo = StepOverCallerInfo.from(location)
if (callerInfo.methodName != null && callerInfo.methodSignature != null && this.callerInfo == callerInfo) {
return token.lineNumber >= 0 && token !in tokensToSkip
} else {
return true
}
}
override fun locationMatches(process: DebugProcessImpl, location: Location): Boolean {
throw IllegalStateException("Should not be called from Kotlin hint")
}
override fun getCallingExpressionLines(): Range<Int>? = null
} | apache-2.0 | 2f54b15751f4f867daf6c8ef9df1a9d6 | 40.902778 | 158 | 0.709881 | 4.787302 | false | false | false | false |
mencattini/eMuLATe | arl/src/main/kotlin/machine/learning/Weights.kt | 1 | 6848 | package machine.learning
import java.util.*
/**
* The weights class. This is the weights for the neural net with the associated methods.
*
* @author Romain Mencattini
*
* @param sizeWindow : the size of the weights vector
* @param index : the current iteration
*/
internal class Weights(private val sizeWindow : Int, private val index: Int) {
var coefficients : DoubleArray
private var oldDiffFt : DoubleArray
var oldAt : Double
var oldBt : Double
private var magnitude : DoubleArray
private var rho : DoubleArray
private val epsilon : Double
init {
// create the oldAt and oldBt
// the default value are different to avoid the division by 0 in weight update
oldAt = 0.0
oldBt = 0.0
// create an array of weight with size of $sizeWindow
// the weight is defined : (w_{0,M}, vThreshold, w_{M+1})
coefficients = DoubleArray(sizeWindow, {0.5})
coefficients[coefficients.lastIndex - 1] = 0.0
// we need to store the diffFt value for the next update
oldDiffFt = kotlin.DoubleArray(sizeWindow)
// rmsprop
magnitude = kotlin.DoubleArray(sizeWindow, {1.0})
rho = DoubleArray(sizeWindow, {0.001})
epsilon = Double.MIN_VALUE
}
/**
* Some kind of constructor. Build a Weights object with the coefficients.
*
* @param coefficients an array of double. This will become our coefficients.
* @param index some kind of timestampe
* @param at the old A(t)
* @param bt the old B(t)
*
* @return a new Weight object with the given coefficient.
*/
private fun Weights(coefficients : DoubleArray, index : Int, at : Double, bt: Double) : Weights {
val weights = Weights(coefficients.size, index)
weights.coefficients = coefficients
oldAt = at
oldBt = bt
return weights
}
/**
* Make the computation to update the coefficients. According to theory it's a gradient ascent.
*
* @param rt is the returns at time t
* @param ft is equal to F(t)
* @param ftMinusOne is equal to F(t-1)
* @param param a parameters object which contains the used values
* @param returns is the array of previous returns.
*
* @return a new Weights object with the new coefficients.
*/
fun updateWeights(rt: Double, ftMinusOne: Double, ft: Double,
param : Parameters, returns: DoubleArray) {
val oldAt = this.oldAt
val oldBt = this.oldBt
val oldDiffFt = this.oldDiffFt.clone()
val diffRt: Double
val diffRtMinusOne: Double
// we compute At, Bt, deltaAt and deltaBt
val deltaAt = (rt - oldAt)
val deltaBt = (rt * rt - oldBt)
val at = oldAt + param.eta * deltaAt
val bt = oldBt + param.eta * deltaBt
val wMplusOne = this.wMplusOne()
if (ft == ftMinusOne) {
diffRt = 0.0
diffRtMinusOne = rt
} else {
val div = (ft - ftMinusOne) / (Math.abs(ft - ftMinusOne))
// the dR_{t} / dF_{t}
diffRt = -param.delta * div
// the dR_{t} / F_{t-1}
diffRtMinusOne = rt + param.delta * div
}
// we start the computation of dF_t / dw_{i,t}
// we need to modify the returns before, so we create a new variable
// we compute the dF_{t} / dw_{i,t} (where it's partial derivation)
val tmpReturns = returns.plus(ftMinusOne).reversed().toDoubleArray()
val diffFt = DoubleArray(oldDiffFt.size)
// according to article, the derivation is dDt / dRt = (B_{t-1} - A_{t-1} * R_t) / (B_{t-1} - A_{t-1}^2)^3/2
var diffDt = (oldBt - oldAt * rt) / Math.pow(Math.abs(oldBt - oldAt * oldAt), 3 / 2.0)
if (diffDt.isNaN()) diffDt = -100.0
// w_{i,t-1} + rho * (diffDt * (diffRt * diffFt + diffRtMinusOne * diffFtMinusOne))
val newCoefficients = DoubleArray(this.coefficients.size)
for (i in this.coefficients.indices) {
// dF_t / dw_{i,t}
diffFt[i] = tmpReturns.getDefault(i, 0.0) + oldDiffFt[i] * wMplusOne
val tmp =(diffDt * (diffRt * diffFt[i] + diffRtMinusOne * oldDiffFt[i]))
// cf. http://ruder.io/optimizing-gradient-descent/index.html#rmsprop
magnitude[i] = magnitude[i] + Math.pow(tmp, 2.0)
newCoefficients[i] = this.coefficients[i] + rho[i] * tmp
// cf. http://ruder.io/optimizing-gradient-descent/index.html#rmsprop
rho[i] /= Math.sqrt(magnitude[i] + epsilon)
}
this.coefficients = newCoefficients.clone()
this.oldAt = at
this.oldBt = bt
// we store the current diffFt as oldDiffFt for the next iteration
this.oldDiffFt = diffFt.clone()
//this.rho = rho.zip(magnitude).map { (first, second) -> first / Math.sqrt(second + epsilon) }.toDoubleArray()
}
/**
* This is an accessor to the private field.
* @return the w_{M+1} weights
*/
fun wMplusOne() : Double {
return coefficients.last()
}
/**
* Rest the weights parameters
*/
fun resetParameters(magnitude:Boolean=false) {
this.oldAt = 0.0
this.oldBt = 0.0
this.oldDiffFt = kotlin.DoubleArray(sizeWindow)
if (magnitude) {
this.magnitude = kotlin.DoubleArray(sizeWindow, {1.0})
this.rho = DoubleArray(sizeWindow, {0.01})
}
}
/**
* This is an accessor to the private field.
* @return the v_t weight
*/
fun vThreshold() : Double {
return coefficients[coefficients.lastIndex - 1]
}
override fun toString(): String {
return "Weights(" +
"coefficients=${Arrays.toString(coefficients.sliceArray(0..coefficients.lastIndex - 2))}," +
"\nvThreshold=${vThreshold()}," +
"\nw_{M+1}=${wMplusOne()})"
}
/**
* This function create a copy of the weight object.
*
* @param coefficients the (w_i, vthreshold, w_{M+1})
* @param index the current iteration
* @param oldAt the old result of at
* @param oldBt the old result of bt
* @param oldDiffFt the old result of ft derivation
*
* @return a new copied object weight
*/
fun copy(index: Int = this.index, coefficients: DoubleArray = this.coefficients,
oldDiffFt : DoubleArray = this.oldDiffFt,oldAt : Double = this.oldAt,
oldBt: Double = this.oldBt): Weights {
val res = this.Weights(coefficients, index, oldAt, oldBt)
res.oldDiffFt = oldDiffFt
return res
}
}
private fun DoubleArray.getDefault(index: Int, d: Double = 0.0): Double {
return if (index < this.size) {
this[index]
} else {
d
}
}
| mit | 3593364b7d8154feffab76cc82fbcaa5 | 33.761421 | 118 | 0.594188 | 3.742077 | false | false | false | false |
siosio/intellij-community | plugins/kotlin/analysis/src/org/jetbrains/kotlin/idea/decompiler/KotlinDecompiledFileViewProvider.kt | 2 | 1789 | // Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.decompiler
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiInvalidElementAccessException
import com.intellij.psi.PsiManager
import com.intellij.psi.SingleRootFileViewProvider
import com.intellij.psi.impl.DebugUtil
import com.intellij.psi.impl.source.PsiFileImpl
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.utils.concurrent.block.LockedClearableLazyValue
class KotlinDecompiledFileViewProvider(
manager: PsiManager,
file: VirtualFile,
physical: Boolean,
private val factory: (KotlinDecompiledFileViewProvider) -> KtDecompiledFile?
) : SingleRootFileViewProvider(manager, file, physical, KotlinLanguage.INSTANCE) {
val content: LockedClearableLazyValue<String> = LockedClearableLazyValue(Any()) {
val psiFile = createFile(manager.project, file, KotlinFileType.INSTANCE)
val text = psiFile?.text ?: ""
DebugUtil.performPsiModification<PsiInvalidElementAccessException>("Invalidating throw-away copy of file that was used for getting text") {
(psiFile as? PsiFileImpl)?.markInvalidated()
}
text
}
override fun createFile(project: Project, file: VirtualFile, fileType: FileType): PsiFile? {
return factory(this)
}
override fun createCopy(copy: VirtualFile) = KotlinDecompiledFileViewProvider(manager, copy, false, factory)
override fun getContents() = content.get()
} | apache-2.0 | 0f8824b451b494e00affa826246c421c | 41.619048 | 158 | 0.778088 | 4.598972 | false | false | false | false |
siosio/intellij-community | plugins/kotlin/frontend-fir/src/org/jetbrains/kotlin/idea/asJava/modifierLists/FirLightModifierList.kt | 2 | 2074 | /*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.idea.asJava
import com.intellij.psi.PsiAnnotation
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiModifierList
import com.intellij.psi.PsiModifierListOwner
import com.intellij.util.IncorrectOperationException
import org.jetbrains.kotlin.asJava.classes.cannotModify
import org.jetbrains.kotlin.asJava.elements.*
import org.jetbrains.kotlin.psi.KtModifierList
import org.jetbrains.kotlin.psi.KtModifierListOwner
internal abstract class FirLightModifierList<out T : KtLightElement<KtModifierListOwner, PsiModifierListOwner>>(
protected val owner: T
) : KtLightElementBase(owner), PsiModifierList, KtLightElement<KtModifierList, PsiModifierListOwner> {
override val clsDelegate: PsiModifierListOwner
get() = invalidAccess()
override val kotlinOrigin: KtModifierList?
get() = owner.kotlinOrigin?.modifierList
override fun getParent() = owner
override fun hasExplicitModifier(name: String) = hasModifierProperty(name)
override fun setModifierProperty(name: String, value: Boolean) = cannotModify()
override fun checkSetModifierProperty(name: String, value: Boolean) = throw IncorrectOperationException()
override fun addAnnotation(qualifiedName: String): PsiAnnotation = cannotModify()
override fun getApplicableAnnotations(): Array<out PsiAnnotation> = annotations
override fun getAnnotations(): Array<out PsiAnnotation> = emptyArray() //TODO()
override fun findAnnotation(qualifiedName: String): PsiAnnotation? = null //TODO()
override fun isEquivalentTo(another: PsiElement?) =
another is FirLightModifierList<*> && owner == another.owner
override fun isWritable() = false
override fun toString() = "Light modifier list of $owner"
abstract override fun equals(other: Any?): Boolean
abstract override fun hashCode(): Int
}
| apache-2.0 | c7ff4987d4206acfcb90f9a15c8b12a5 | 39.666667 | 115 | 0.776278 | 5.009662 | false | false | false | false |
kivensolo/UiUsingListView | module-Eyepetizer/src/main/java/com/zeke/eyepetizer/adapter/VideoPlayAdapter.kt | 1 | 7792 | package com.zeke.eyepetizer.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.FlexboxLayout
import com.google.gson.Gson
import com.kingz.module.common.utils.image.GlideLoader
import com.zeke.eyepetizer.anim.TiaoZiUtil
import com.zeke.eyepetizer.bean.Data
import com.zeke.eyepetizer.bean.Item
import com.zeke.eyepetizer.bean.cards.item.VideoSmallCard
import com.zeke.kangaroo.utils.TimeUtils
import com.zeke.moudle_eyepetizer.R
/**
* author:ZekeWang
* date:2021/6/8
* description: 视频播放页的数据适配器
*/
class VideoPlayAdapter : RecyclerView.Adapter<BaseViewHolder>() {
private var mContext: Context? = null
//data
var mHeaderData: Data? = null
var mRelatedData = ArrayList<Item>()
lateinit var onRelatedItemClick: (item: Item) -> Unit
enum class ItemType(var value: Int) {
TYPE_HEADER(0),
TYPE_RELATED(1),
TYPE_THE_END(2);
}
fun setData(headData: Data, relatedData: List<Item>) {
this.mHeaderData = headData
this.mRelatedData = relatedData as ArrayList<Item>
this.notifyDataSetChanged()
}
override fun getItemCount(): Int {
return 1 + mRelatedData.size + 1
}
// --------------------- Create & Bind View holder ---------------------
/**
* 根据UI类型创建Holder
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
mContext = parent.context
val inflater = LayoutInflater.from(parent.context)
fun getHolder(@LayoutRes resource: Int): BaseViewHolder {
return BaseViewHolder(inflater.inflate(resource, parent, false))
}
val viewHolder =
when (viewType) {
ItemType.TYPE_HEADER.value -> getHolder(R.layout.item_bean_for_client_card)
ItemType.TYPE_RELATED.value -> getHolder(R.layout.item_video_small_card)
ItemType.TYPE_THE_END.value -> getHolder(R.layout.template_the_end)
else -> throw Exception("invalid view type")
}
return BaseViewHolder(viewHolder.itemView)
}
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
when (getItemViewType(position)) {
ItemType.TYPE_HEADER.value -> initHeaderView(holder)
ItemType.TYPE_RELATED.value -> initRelatedView(holder, mRelatedData[position-1])
ItemType.TYPE_THE_END.value -> initTheEndView(holder)
}
}
override fun getItemViewType(position: Int): Int {
return when (position) {
0 -> ItemType.TYPE_HEADER.value
in 1..mRelatedData.size -> ItemType.TYPE_RELATED.value
else -> ItemType.TYPE_THE_END.value
}
}
// --------------------- Create & Bind View holder ---------------------
private fun initHeaderView(holder: BaseViewHolder) {
//视频标题
val tvVideoTitleView = holder.getView<TextView>(R.id.tvVideoTitle)
tvVideoTitleView.text = mHeaderData?.title
//视频分类
holder.getView<TextView>(R.id.tvCategory).text = "#${mHeaderData?.category}"
//视频描述
holder.getView<TextView>(R.id.tvVideoDescription).text = mHeaderData?.description
//收藏总数
val tvCollectionCountView = holder.getView<TextView>(R.id.tvCollectionCount)
tvCollectionCountView.text = mHeaderData?.consumption?.collectionCount.toString()
//分享总数
val tvShareView = holder.getView<TextView>(R.id.tvShare)
tvShareView.text = mHeaderData?.consumption?.shareCount.toString()
//评论回复数量
val tvReplyView = holder.getView<TextView>(R.id.tvReply)
tvReplyView.text = mHeaderData?.consumption?.replyCount.toString()
val tvPreloadView = holder.getView<TextView>(R.id.tvPreload)
val tvAuthorView = holder.getView<TextView>(R.id.tvAuthor)
val tvSloganView = holder.getView<TextView>(R.id.tvSlogan)
val ivAvatarView = holder.getView<ImageView>(R.id.ivAvatar)
//标签容器(图片+文字)
val tagsContainer = holder.getView<FlexboxLayout>(R.id.tagsContainer)
tagsContainer.removeAllViews()
mHeaderData?.tags?.forEach { itemTag ->
val itemView = LayoutInflater.from(mContext)
.inflate(R.layout.video_tag, tagsContainer, false)
val ivTag = itemView.findViewById<ImageView>(R.id.ivTag)
val tvTag = itemView.findViewById<TextView>(R.id.tvTag)
tvTag.text = "#" + itemTag.name + "#"
GlideLoader.loadNetImageWithCornerAndShade(
mContext!!,
ivTag,
itemTag.headerImage,
placeHolderId = R.drawable.corner_4_solid_dark_light
)
tagsContainer.addView(itemView)
tvAuthorView.text = mHeaderData?.author?.name
tvSloganView.text = mHeaderData?.author?.description
GlideLoader.loadNetCircleImage(mContext!!, ivAvatarView, mHeaderData?.author?.icon)
}
//init Animate
val translationAnim = TranslateAnimation(0f, 0f, -80f, 0f)
translationAnim.duration = 400
tvShareView.startAnimation(translationAnim)
tvReplyView.startAnimation(translationAnim)
tvPreloadView.startAnimation(translationAnim)
tvCollectionCountView.startAnimation(translationAnim)
TiaoZiUtil(tvVideoTitleView, mHeaderData?.title, 50)
}
private fun initRelatedView(holder: BaseViewHolder, item: Item) {
//
// //init data
val jsonObject = item.data
val videoSmallCard = Gson().fromJson(jsonObject, VideoSmallCard::class.java)
val videoTitle =
videoSmallCard.title //视频标题
val videoFeedUrl =
videoSmallCard.cover.detail //视频封面Url
val videoCategory =
"#" + videoSmallCard?.category //视频类别
val videoDuration =
TimeUtils.generateTime(videoSmallCard.duration * 1000.toLong()) //视频时长
holder.getView<TextView>(R.id.tvTitle)?.apply {
this.text = videoTitle
this.setTextColor(resources.getColor(R.color.white))
}
holder.getView<TextView>(R.id.tvCatogory).apply {
this.text = videoCategory
this.setTextColor(resources.getColor(R.color.white))
}
holder.getView<TextView>(R.id.tvVideoDuration).apply {
this.text = videoDuration
}
val ivFeed = holder.getView<ImageView>(R.id.ivFeed)
GlideLoader.loadNetImageWithCorner(
mContext!!, ivFeed,
videoFeedUrl, placeHolderId = R.drawable.corner_4_solid_dark_light
)
holder.itemView.setOnClickListener {
onRelatedItemClick(item)
}
}
//没有更多数据,到底部的提示ItemView
private fun initTheEndView(holder: BaseViewHolder) {
holder.getView<TextView>(R.id.tvEnd).apply {
this.typeface = android.graphics.Typeface.createFromAsset(mContext!!.assets, "fonts/Lobster-1.4.otf")
this.setTextColor(resources.getColor(R.color.white))
(parent as View).setBackgroundColor(android.graphics.Color.TRANSPARENT)
}
}
} | gpl-2.0 | e7b0527865d9f63bc45622b5367ed256 | 38.518135 | 113 | 0.642801 | 4.279461 | false | false | false | false |
980f/droid | android/src/main/kotlin/pers/hal42/android/LinearSlider.kt | 1 | 841 | package pers.hal42.android
import android.content.Context
import android.widget.SeekBar
/**
* Copyright (C) by andyh created on 11/9/12 at 3:18 PM
*/
class LinearSlider(context: Context) : SeekBar(context) {
init {
max = quanta.toInt()
}
fun setValue(dee: Double) {
if (dee < 0) {
progress = 0
} else if (dee > 1.0) {
progress = max
} else {
progress = (quanta * dee).toInt()
}
}
val value: Float
get() {
val dee = progress.toFloat()
return dee / quanta
}
fun getValue(scalar: Float): Float {
val dee = progress.toFloat()
return dee * scalar / quanta
}
fun getValue(min: Float, max: Float): Float {
val dee = progress.toFloat()
return min + dee * (max - min) / quanta
}
companion object {
val quanta = (1 shl 22).toFloat()//
}
}
| mit | 4e9222ac3f3407dd16ac4060ff02cc57 | 17.282609 | 57 | 0.588585 | 3.337302 | false | false | false | false |
code-disaster/lwjgl3 | modules/lwjgl/cuda/src/templates/kotlin/cuda/templates/CU.kt | 4 | 522709 | /*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
package cuda.templates
import cuda.*
import org.lwjgl.generator.*
val CU = "CU".nativeClass(Module.CUDA, prefix = "CU", binding = NVCUDA_BINDING) {
javaImport("static org.lwjgl.cuda.CUDA.*")
documentation =
"""
Contains bindings to the <a href="https://docs.nvidia.com/cuda/cuda-driver-api/index.html">CUDA Driver API</a>.
Functionality up to CUDA version 3.2, which is the minimum version compatible with the LWJGL bindings, is guaranteed to be available. Functions
introduced after CUDA 3.2 may or may not be missing, depending on the CUDA version available at runtime.
"""
IntConstant("", "IPC_HANDLE_SIZE".."64")
EnumConstant(
"CUDA Ipc Mem Flags. ({@code CUipcMem_flags})",
"IPC_MEM_LAZY_ENABLE_PEER_ACCESS".enum("Automatically enable peer access between remote devices as needed", 0x1)
)
EnumConstant(
"CUDA Mem Attach Flags. ({@code CUmemAttach_flags})",
"MEM_ATTACH_GLOBAL".enum("Memory can be accessed by any stream on any device", 0x1),
"MEM_ATTACH_HOST".enum("Memory cannot be accessed by any stream on any device", 0x2),
"MEM_ATTACH_SINGLE".enum("Memory can only be accessed by a single stream on the associated device", 0x4)
)
EnumConstant(
"Context creation flags. ({@code CUctx_flags})",
"CTX_SCHED_AUTO".enum("Automatic scheduling", 0x00),
"CTX_SCHED_SPIN".enum("Set spin as default scheduling", 0x01),
"CTX_SCHED_YIELD".enum("Set yield as default scheduling", 0x02),
"CTX_SCHED_BLOCKING_SYNC".enum("Set blocking synchronization as default scheduling", 0x04),
"CTX_BLOCKING_SYNC".enum(
"Set blocking synchronization as default scheduling. This flag was deprecated as of CUDA 4.0 and was replaced with #CTX_SCHED_BLOCKING_SYNC.",
0x04
),
"CTX_SCHED_MASK".enum("", 0x07),
"CTX_MAP_HOST".enum(
"""
This flag was deprecated as of CUDA 11.0 and it no longer has any effect.
All contexts as of CUDA 3.2 behave as though the flag is enabled.
""",
0x08
),
"CTX_LMEM_RESIZE_TO_MAX".enum("Keep local memory allocation after launch", 0x10),
"CTX_FLAGS_MASK".enum("", 0x1f)
)
EnumConstant(
"Stream creation flags. ({@code CUstream_flags})",
"STREAM_DEFAULT".enum("Default stream flag", 0x0),
"STREAM_NON_BLOCKING".enum("Stream does not synchronize with stream 0 (the #NULL stream)", 0x1)
)
LongConstant(
"""
Legacy stream handle.
Stream handle that can be passed as a {@code CUstream} to use an implicit stream with legacy synchronization behavior.
""",
"STREAM_LEGACY"..0x1L
)
LongConstant(
"""
Per-thread stream handle.
Stream handle that can be passed as a {@code CUstream} to use an implicit stream with per-thread synchronization behavior.
""",
"STREAM_PER_THREAD"..0x2L
)
EnumConstant(
"Event creation flags. ({@code CUevent_flags})",
"EVENT_DEFAULT".enum("Default event flag", 0x0),
"EVENT_BLOCKING_SYNC".enum("Event uses blocking synchronization", 0x1),
"EVENT_DISABLE_TIMING".enum("Event will not record timing data", 0x2),
"EVENT_INTERPROCESS".enum("Event is suitable for interprocess use. #EVENT_DISABLE_TIMING must be set", 0x4)
)
EnumConstant(
"Event record flags. ({@code CUevent_record_flags})",
"EVENT_RECORD_DEFAULT".enum("Default event record flag", 0x0),
"EVENT_RECORD_EXTERNAL".enum(
"""
When using stream capture, create an event record node instead of the default behavior.
This flag is invalid when used outside of capture.
"""
)
)
EnumConstant(
"Event wait flags. ({@code CUevent_wait_flags})",
"EVENT_WAIT_DEFAULT".enum("Default event wait flag", 0x0),
"EVENT_WAIT_EXTERNAL".enum(
"""
When using stream capture, create an event wait node instead of the default behavior.
This flag is invalid when used outside of capture.
"""
)
)
EnumConstant(
"Flags for #StreamWaitValue32() and #StreamWaitValue64(). ({@code CUstreamWaitValue_flags})",
"STREAM_WAIT_VALUE_GEQ".enum(
"""
Wait until {@code (int32_t)(*addr - value) >= 0} (or {@code int64_t} for 64 bit values). Note this is a cyclic comparison which ignores
wraparound. (Default behavior.)
""",
0x0
),
"STREAM_WAIT_VALUE_EQ".enum("Wait until {@code *addr == value}.", 0x1),
"STREAM_WAIT_VALUE_AND".enum("Wait until {@code (*addr & value) != 0}.", 0x2),
"STREAM_WAIT_VALUE_NOR".enum(
"""
Wait until {@code ~(*addr | value) != 0}. Support for this operation can be queried with #DeviceGetAttribute() and
#DEVICE_ATTRIBUTE_CAN_USE_STREAM_WAIT_VALUE_NOR.
""",
0x3
),
"STREAM_WAIT_VALUE_FLUSH".enum(
"""
Follow the wait operation with a flush of outstanding remote writes.
This means that, if a remote write operation is guaranteed to have reached the device before the wait can be satisfied, that write is guaranteed to
be visible to downstream device work. The device is permitted to reorder remote writes internally. For example, this flag would be required if two
remote writes arrive in a defined order, the wait is satisfied by the second write, and downstream work needs to observe the first write.
Support for this operation is restricted to selected platforms and can be queried with {@code CU_DEVICE_ATTRIBUTE_CAN_USE_WAIT_VALUE_FLUSH}.
""",
"1<<30"
)
)
EnumConstant(
"Flags for #StreamWriteValue32(). ({@code CUstreamWriteValue_flags})",
"STREAM_WRITE_VALUE_DEFAULT".enum("Default behavior", 0x0),
"STREAM_WRITE_VALUE_NO_MEMORY_BARRIER".enum(
"""
Permits the write to be reordered with writes which were issued before it, as a performance optimization.
Normally, #StreamWriteValue32() will provide a memory fence before the write, which has similar semantics to {@code __threadfence_system()} but is
scoped to the stream rather than a CUDA thread.
""",
0x1
)
)
EnumConstant(
"Operations for #StreamBatchMemOp(). ({@code CUstreamBatchMemOpType})",
"STREAM_MEM_OP_WAIT_VALUE_32".enum("Represents a #StreamWaitValue32() operation", 1),
"STREAM_MEM_OP_WRITE_VALUE_32".enum("Represents a #StreamWriteValue32() operation", 2),
"STREAM_MEM_OP_WAIT_VALUE_64".enum("Represents a #StreamWaitValue64() operation", 4),
"STREAM_MEM_OP_WRITE_VALUE_64".enum("Represents a #StreamWriteValue64() operation", 5),
"STREAM_MEM_OP_FLUSH_REMOTE_WRITES".enum("This has the same effect as #STREAM_WAIT_VALUE_FLUSH, but as a standalone operation.", 3)
)
EnumConstant(
"Occupancy calculator flag. ({@code CUoccupancy_flags})",
"OCCUPANCY_DEFAULT".enum("Default behavior", 0x0),
"OCCUPANCY_DISABLE_CACHING_OVERRIDE".enum("Assume global caching is enabled and cannot be automatically turned off", 0x1)
)
EnumConstant(
"Flags for #StreamUpdateCaptureDependencies()). ({@code CUstreamUpdateCaptureDependencies_flags})",
"STREAM_ADD_CAPTURE_DEPENDENCIES".enum("Add new nodes to the dependency set", 0x0),
"STREAM_SET_CAPTURE_DEPENDENCIES".enum("Replace the dependency set with the new nodes", 0x1)
)
EnumConstant(
"Array formats. ({@code CUarray_format})",
"AD_FORMAT_UNSIGNED_INT8".enum("Unsigned 8-bit integers", "0x01"),
"AD_FORMAT_UNSIGNED_INT16".enum("Unsigned 16-bit integers", "0x02"),
"AD_FORMAT_UNSIGNED_INT32".enum("Unsigned 32-bit integers", "0x03"),
"AD_FORMAT_SIGNED_INT8".enum("Signed 8-bit integers", "0x08"),
"AD_FORMAT_SIGNED_INT16".enum("Signed 16-bit integers", "0x09"),
"AD_FORMAT_SIGNED_INT32".enum("Signed 32-bit integers", "0x0a"),
"AD_FORMAT_HALF".enum("16-bit floating point", "0x10"),
"AD_FORMAT_FLOAT".enum("32-bit floating point", "0x20"),
"AD_FORMAT_NV12".enum("8-bit YUV planar format, with 4:2:0 sampling", "0xb0"),
"AD_FORMAT_UNORM_INT8X1".enum("1 channel unsigned 8-bit normalized integer", "0xc0"),
"AD_FORMAT_UNORM_INT8X2".enum("2 channel unsigned 8-bit normalized integer", "0xc1"),
"AD_FORMAT_UNORM_INT8X4".enum("4 channel unsigned 8-bit normalized integer", "0xc2"),
"AD_FORMAT_UNORM_INT16X1".enum("1 channel unsigned 16-bit normalized integer", "0xc3"),
"AD_FORMAT_UNORM_INT16X2".enum("2 channel unsigned 16-bit normalized integer", "0xc4"),
"AD_FORMAT_UNORM_INT16X4".enum("4 channel unsigned 16-bit normalized integer", "0xc5"),
"AD_FORMAT_SNORM_INT8X1".enum("1 channel signed 8-bit normalized integer", "0xc6"),
"AD_FORMAT_SNORM_INT8X2".enum("2 channel signed 8-bit normalized integer", "0xc7"),
"AD_FORMAT_SNORM_INT8X4".enum("4 channel signed 8-bit normalized integer", "0xc8"),
"AD_FORMAT_SNORM_INT16X1".enum("1 channel signed 16-bit normalized integer", "0xc9"),
"AD_FORMAT_SNORM_INT16X2".enum("2 channel signed 16-bit normalized integer", "0xca"),
"AD_FORMAT_SNORM_INT16X4".enum("4 channel signed 16-bit normalized integer", "0xcb"),
"AD_FORMAT_BC1_UNORM".enum("4 channel unsigned normalized block-compressed (BC1 compression) format", "0x91"),
"AD_FORMAT_BC1_UNORM_SRGB".enum("4 channel unsigned normalized block-compressed (BC1 compression) format with sRGB encoding", "0x92"),
"AD_FORMAT_BC2_UNORM".enum("4 channel unsigned normalized block-compressed (BC2 compression) format", "0x93"),
"AD_FORMAT_BC2_UNORM_SRGB".enum("4 channel unsigned normalized block-compressed (BC2 compression) format with sRGB encoding", "0x94"),
"AD_FORMAT_BC3_UNORM".enum("4 channel unsigned normalized block-compressed (BC3 compression) format", "0x95"),
"AD_FORMAT_BC3_UNORM_SRGB".enum("4 channel unsigned normalized block-compressed (BC3 compression) format with sRGB encoding", "0x96"),
"AD_FORMAT_BC4_UNORM".enum("1 channel unsigned normalized block-compressed (BC4 compression) format", "0x97"),
"AD_FORMAT_BC4_SNORM".enum("1 channel signed normalized block-compressed (BC4 compression) format", "0x98"),
"AD_FORMAT_BC5_UNORM".enum("2 channel unsigned normalized block-compressed (BC5 compression) format", "0x99"),
"AD_FORMAT_BC5_SNORM".enum("2 channel signed normalized block-compressed (BC5 compression) format", "0x9a"),
"AD_FORMAT_BC6H_UF16".enum("3 channel unsigned half-float block-compressed (BC6H compression) format", "0x9b"),
"AD_FORMAT_BC6H_SF16".enum("3 channel signed half-float block-compressed (BC6H compression) format", "0x9c"),
"AD_FORMAT_BC7_UNORM".enum("4 channel unsigned normalized block-compressed (BC7 compression) format", "0x9d"),
"AD_FORMAT_BC7_UNORM_SRGB".enum("4 channel unsigned normalized block-compressed (BC7 compression) format with sRGB encoding", "0x9e")
)
EnumConstant(
"Texture reference addressing modes. ({@code CUaddress_mode})",
"TR_ADDRESS_MODE_WRAP".enum("Wrapping address mode", 0),
"TR_ADDRESS_MODE_CLAMP".enum("Clamp to edge address mode", 1),
"TR_ADDRESS_MODE_MIRROR".enum("Mirror address mode", 2),
"TR_ADDRESS_MODE_BORDER".enum("Border address mode", 3)
)
EnumConstant(
"Texture reference filtering modes. ({@code CUfilter_mode})",
"TR_FILTER_MODE_POINT".enum("Point filter mode", 0),
"TR_FILTER_MODE_LINEAR".enum("Linear filter mode", 1)
)
EnumConstant(
"Device properties. ({@code CUdevice_attribute})",
"DEVICE_ATTRIBUTE_MAX_THREADS_PER_BLOCK".enum("Maximum number of threads per block", "1"),
"DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_X".enum("Maximum block dimension X"),
"DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Y".enum("Maximum block dimension Y"),
"DEVICE_ATTRIBUTE_MAX_BLOCK_DIM_Z".enum("Maximum block dimension Z"),
"DEVICE_ATTRIBUTE_MAX_GRID_DIM_X".enum("Maximum grid dimension X"),
"DEVICE_ATTRIBUTE_MAX_GRID_DIM_Y".enum("Maximum grid dimension Y"),
"DEVICE_ATTRIBUTE_MAX_GRID_DIM_Z".enum("Maximum grid dimension Z"),
"DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK".enum("Maximum shared memory available per block in bytes"),
"DEVICE_ATTRIBUTE_SHARED_MEMORY_PER_BLOCK".enum("Deprecated, use #DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK", "8"),
"DEVICE_ATTRIBUTE_TOTAL_CONSTANT_MEMORY".enum("Memory available on device for __constant__ variables in a CUDA C kernel in bytes"),
"DEVICE_ATTRIBUTE_WARP_SIZE".enum("Warp size in threads"),
"DEVICE_ATTRIBUTE_MAX_PITCH".enum("Maximum pitch in bytes allowed by memory copies"),
"DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_BLOCK".enum("Maximum number of 32-bit registers available per block"),
"DEVICE_ATTRIBUTE_REGISTERS_PER_BLOCK".enum("Deprecated, use #DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_BLOCK", "12"),
"DEVICE_ATTRIBUTE_CLOCK_RATE".enum("Typical clock frequency in kilohertz"),
"DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT".enum("Alignment requirement for textures"),
"DEVICE_ATTRIBUTE_GPU_OVERLAP".enum(
"Device can possibly copy memory and execute a kernel concurrently. Deprecated. Use instead #DEVICE_ATTRIBUTE_ASYNC_ENGINE_COUNT."
),
"DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT".enum("Number of multiprocessors on device"),
"DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT".enum("Specifies whether there is a run time limit on kernels"),
"DEVICE_ATTRIBUTE_INTEGRATED".enum("Device is integrated with host memory"),
"DEVICE_ATTRIBUTE_CAN_MAP_HOST_MEMORY".enum("Device can map host memory into CUDA address space"),
"DEVICE_ATTRIBUTE_COMPUTE_MODE".enum("Compute mode (See {@code CUcomputemode} for details)"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_WIDTH".enum("Maximum 1D texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_WIDTH".enum("Maximum 2D texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_HEIGHT".enum("Maximum 2D texture height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_WIDTH".enum("Maximum 3D texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_HEIGHT".enum("Maximum 3D texture height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_DEPTH".enum("Maximum 3D texture depth"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LAYERED_WIDTH".enum("Maximum 2D layered texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LAYERED_HEIGHT".enum("Maximum 2D layered texture height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LAYERED_LAYERS".enum("Maximum layers in a 2D layered texture"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_ARRAY_WIDTH".enum("Deprecated, use #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LAYERED_WIDTH", "27"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_ARRAY_HEIGHT".enum("Deprecated, use #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LAYERED_HEIGHT"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_ARRAY_NUMSLICES".enum("Deprecated, use #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LAYERED_LAYERS"),
"DEVICE_ATTRIBUTE_SURFACE_ALIGNMENT".enum("Alignment requirement for surfaces"),
"DEVICE_ATTRIBUTE_CONCURRENT_KERNELS".enum("Device can possibly execute multiple kernels concurrently"),
"DEVICE_ATTRIBUTE_ECC_ENABLED".enum("Device has ECC support enabled"),
"DEVICE_ATTRIBUTE_PCI_BUS_ID".enum("PCI bus ID of the device"),
"DEVICE_ATTRIBUTE_PCI_DEVICE_ID".enum("PCI device ID of the device"),
"DEVICE_ATTRIBUTE_TCC_DRIVER".enum("Device is using TCC driver model"),
"DEVICE_ATTRIBUTE_MEMORY_CLOCK_RATE".enum("Peak memory clock frequency in kilohertz"),
"DEVICE_ATTRIBUTE_GLOBAL_MEMORY_BUS_WIDTH".enum("Global memory bus width in bits"),
"DEVICE_ATTRIBUTE_L2_CACHE_SIZE".enum("Size of L2 cache in bytes"),
"DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR".enum("Maximum resident threads per multiprocessor"),
"DEVICE_ATTRIBUTE_ASYNC_ENGINE_COUNT".enum("Number of asynchronous engines"),
"DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING".enum("Device shares a unified address space with the host"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_LAYERED_WIDTH".enum("Maximum 1D layered texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_LAYERED_LAYERS".enum("Maximum layers in a 1D layered texture"),
"DEVICE_ATTRIBUTE_CAN_TEX2D_GATHER".enum("Deprecated, do not use."),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_GATHER_WIDTH".enum("Maximum 2D texture width if #CUDA_ARRAY3D_TEXTURE_GATHER is set"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_GATHER_HEIGHT".enum("Maximum 2D texture height if #CUDA_ARRAY3D_TEXTURE_GATHER is set"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_WIDTH_ALTERNATE".enum("Alternate maximum 3D texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_HEIGHT_ALTERNATE".enum("Alternate maximum 3D texture height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE3D_DEPTH_ALTERNATE".enum("Alternate maximum 3D texture depth"),
"DEVICE_ATTRIBUTE_PCI_DOMAIN_ID".enum("PCI domain ID of the device"),
"DEVICE_ATTRIBUTE_TEXTURE_PITCH_ALIGNMENT".enum("Pitch alignment requirement for textures"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURECUBEMAP_WIDTH".enum("Maximum cubemap texture width/height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURECUBEMAP_LAYERED_WIDTH".enum("Maximum cubemap layered texture width/height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURECUBEMAP_LAYERED_LAYERS".enum("Maximum layers in a cubemap layered texture"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE1D_WIDTH".enum("Maximum 1D surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_WIDTH".enum("Maximum 2D surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_HEIGHT".enum("Maximum 2D surface height"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE3D_WIDTH".enum("Maximum 3D surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE3D_HEIGHT".enum("Maximum 3D surface height"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE3D_DEPTH".enum("Maximum 3D surface depth"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE1D_LAYERED_WIDTH".enum("Maximum 1D layered surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE1D_LAYERED_LAYERS".enum("Maximum layers in a 1D layered surface"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_LAYERED_WIDTH".enum("Maximum 2D layered surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_LAYERED_HEIGHT".enum("Maximum 2D layered surface height"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACE2D_LAYERED_LAYERS".enum("Maximum layers in a 2D layered surface"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACECUBEMAP_WIDTH".enum("Maximum cubemap surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACECUBEMAP_LAYERED_WIDTH".enum("Maximum cubemap layered surface width"),
"DEVICE_ATTRIBUTE_MAXIMUM_SURFACECUBEMAP_LAYERED_LAYERS".enum("Maximum layers in a cubemap layered surface"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_LINEAR_WIDTH".enum(
"Deprecated, do not use. Use {@code cudaDeviceGetTexture1DLinearMaxWidth()} or #DeviceGetTexture1DLinearMaxWidth() instead."
),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_WIDTH".enum("Maximum 2D linear texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_HEIGHT".enum("Maximum 2D linear texture height"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_PITCH".enum("Maximum 2D linear texture pitch in bytes"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_MIPMAPPED_WIDTH".enum("Maximum mipmapped 2D texture width"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_MIPMAPPED_HEIGHT".enum("Maximum mipmapped 2D texture height"),
"DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR".enum("Major compute capability version number"),
"DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR".enum("Minor compute capability version number"),
"DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_MIPMAPPED_WIDTH".enum("Maximum mipmapped 1D texture width"),
"DEVICE_ATTRIBUTE_STREAM_PRIORITIES_SUPPORTED".enum("Device supports stream priorities"),
"DEVICE_ATTRIBUTE_GLOBAL_L1_CACHE_SUPPORTED".enum("Device supports caching globals in L1"),
"DEVICE_ATTRIBUTE_LOCAL_L1_CACHE_SUPPORTED".enum("Device supports caching locals in L1"),
"DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_MULTIPROCESSOR".enum("Maximum shared memory available per multiprocessor in bytes"),
"DEVICE_ATTRIBUTE_MAX_REGISTERS_PER_MULTIPROCESSOR".enum("Maximum number of 32-bit registers available per multiprocessor"),
"DEVICE_ATTRIBUTE_MANAGED_MEMORY".enum("Device can allocate managed memory on this system"),
"DEVICE_ATTRIBUTE_MULTI_GPU_BOARD".enum("Device is on a multi-GPU board"),
"DEVICE_ATTRIBUTE_MULTI_GPU_BOARD_GROUP_ID".enum("Unique id for a group of devices on the same multi-GPU board"),
"DEVICE_ATTRIBUTE_HOST_NATIVE_ATOMIC_SUPPORTED".enum(
"""
Link between the device and the host supports native atomic operations (this is a placeholder attribute, and is not supported on any current
hardware)
"""
),
"DEVICE_ATTRIBUTE_SINGLE_TO_DOUBLE_PRECISION_PERF_RATIO".enum(
"Ratio of single precision performance (in floating-point operations per second) to double precision performance"
),
"DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS".enum("Device supports coherently accessing pageable memory without calling cudaHostRegister on it"),
"DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS".enum("Device can coherently access managed memory concurrently with the CPU"),
"DEVICE_ATTRIBUTE_COMPUTE_PREEMPTION_SUPPORTED".enum("Device supports compute preemption."),
"DEVICE_ATTRIBUTE_CAN_USE_HOST_POINTER_FOR_REGISTERED_MEM".enum("Device can access host registered memory at the same virtual address as the CPU"),
"DEVICE_ATTRIBUTE_CAN_USE_STREAM_MEM_OPS".enum("#StreamBatchMemOp() and related APIs are supported."),
"DEVICE_ATTRIBUTE_CAN_USE_64_BIT_STREAM_MEM_OPS".enum("64-bit operations are supported in #StreamBatchMemOp() and related APIs."),
"DEVICE_ATTRIBUTE_CAN_USE_STREAM_WAIT_VALUE_NOR".enum("#STREAM_WAIT_VALUE_NOR is supported."),
"DEVICE_ATTRIBUTE_COOPERATIVE_LAUNCH".enum("Device supports launching cooperative kernels via #LaunchCooperativeKernel()"),
"DEVICE_ATTRIBUTE_COOPERATIVE_MULTI_DEVICE_LAUNCH".enum("Deprecated, #LaunchCooperativeKernelMultiDevice() is deprecated."),
"DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK_OPTIN".enum("Maximum optin shared memory per block"),
"DEVICE_ATTRIBUTE_CAN_FLUSH_REMOTE_WRITES".enum(
"""
The #STREAM_WAIT_VALUE_FLUSH flag and the #STREAM_MEM_OP_FLUSH_REMOTE_WRITES MemOp are supported on the device. See {@code CUDA_MEMOP} for
additional details.
"""
),
"DEVICE_ATTRIBUTE_HOST_REGISTER_SUPPORTED".enum("Device supports host memory registration via {@code cudaHostRegister()}."),
"DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLES".enum("Device accesses pageable memory via the host's page tables."),
"DEVICE_ATTRIBUTE_DIRECT_MANAGED_MEM_ACCESS_FROM_HOST".enum("The host can directly access managed memory on the device without migration."),
"DEVICE_ATTRIBUTE_VIRTUAL_ADDRESS_MANAGEMENT_SUPPORTED".enum("Deprecated, Use #DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED"),
"DEVICE_ATTRIBUTE_VIRTUAL_MEMORY_MANAGEMENT_SUPPORTED".enum(
"Device supports virtual memory management APIs like #MemAddressReserve(), #MemCreate(), #MemMap() and related APIs",
"102"
),
"DEVICE_ATTRIBUTE_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR_SUPPORTED".enum(
"Device supports exporting memory to a posix file descriptor with #MemExportToShareableHandle(), if requested via #MemCreate()"
),
"DEVICE_ATTRIBUTE_HANDLE_TYPE_WIN32_HANDLE_SUPPORTED".enum(
"Device supports exporting memory to a Win32 NT handle with #MemExportToShareableHandle(), if requested via #MemCreate()"
),
"DEVICE_ATTRIBUTE_HANDLE_TYPE_WIN32_KMT_HANDLE_SUPPORTED".enum(
"Device supports exporting memory to a Win32 KMT handle with #MemExportToShareableHandle(), if requested via #MemCreate()"
),
"DEVICE_ATTRIBUTE_MAX_BLOCKS_PER_MULTIPROCESSOR".enum("Maximum number of blocks per multiprocessor"),
"DEVICE_ATTRIBUTE_GENERIC_COMPRESSION_SUPPORTED".enum("Device supports compression of memory"),
"DEVICE_ATTRIBUTE_MAX_PERSISTING_L2_CACHE_SIZE".enum("Maximum L2 persisting lines capacity setting in bytes."),
"DEVICE_ATTRIBUTE_MAX_ACCESS_POLICY_WINDOW_SIZE".enum("Maximum value of ##CUaccessPolicyWindow{@code {@code num_bytes}}."),
"DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_WITH_CUDA_VMM_SUPPORTED".enum("Device supports specifying the GPUDirect RDMA flag with #MemCreate()"),
"DEVICE_ATTRIBUTE_RESERVED_SHARED_MEMORY_PER_BLOCK".enum("Shared memory reserved by CUDA driver per block in bytes"),
"DEVICE_ATTRIBUTE_SPARSE_CUDA_ARRAY_SUPPORTED".enum("Device supports sparse CUDA arrays and sparse CUDA mipmapped arrays"),
"DEVICE_ATTRIBUTE_READ_ONLY_HOST_REGISTER_SUPPORTED".enum(
"Device supports using the #MemHostRegister() flag #MEMHOSTREGISTER_READ_ONLY to register memory that must be mapped as read-only to the GPU"
),
"DEVICE_ATTRIBUTE_TIMELINE_SEMAPHORE_INTEROP_SUPPORTED".enum("External timeline semaphore interop is supported on the device"),
"DEVICE_ATTRIBUTE_MEMORY_POOLS_SUPPORTED".enum("Device supports using the #MemAllocAsync() and {@code cuMemPool*} family of APIs"),
"DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_SUPPORTED".enum(
"Device supports GPUDirect RDMA APIs, like nvidia_p2p_get_pages (see ${url("https://docs.nvidia.com/cuda/gpudirect-rdma")} for more information)"
),
"DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_FLUSH_WRITES_OPTIONS".enum(
"""
The returned attribute shall be interpreted as a bitmask, where the individual bits are described by the {@code CUflushGPUDirectRDMAWritesOptions}
enum
"""
),
"DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_WRITES_ORDERING".enum(
"""
GPUDirect RDMA writes to the device do not need to be flushed for consumers within the scope indicated by the returned attribute. See
{@code CUGPUDirectRDMAWritesOrdering} for the numerical values returned here.
"""
),
"DEVICE_ATTRIBUTE_MEMPOOL_SUPPORTED_HANDLE_TYPES".enum("Handle types supported with mempool based IPC")
)
EnumConstant(
"""
Pointer information. ({@code CUpointer_attribute})
""",
"POINTER_ATTRIBUTE_CONTEXT".enum("The {@code CUcontext} on which a pointer was allocated or registered", "1"),
"POINTER_ATTRIBUTE_MEMORY_TYPE".enum("The {@code CUmemorytype} describing the physical location of a pointer"),
"POINTER_ATTRIBUTE_DEVICE_POINTER".enum("The address at which a pointer's memory may be accessed on the device"),
"POINTER_ATTRIBUTE_HOST_POINTER".enum("The address at which a pointer's memory may be accessed on the host"),
"POINTER_ATTRIBUTE_P2P_TOKENS".enum("A pair of tokens for use with the {@code nv-p2p.h} Linux kernel interface"),
"POINTER_ATTRIBUTE_SYNC_MEMOPS".enum("Synchronize every synchronous memory operation initiated on this region"),
"POINTER_ATTRIBUTE_BUFFER_ID".enum("A process-wide unique ID for an allocated memory region"),
"POINTER_ATTRIBUTE_IS_MANAGED".enum("Indicates if the pointer points to managed memory"),
"POINTER_ATTRIBUTE_DEVICE_ORDINAL".enum("A device ordinal of a device on which a pointer was allocated or registered"),
"POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE".enum(
"1 if this pointer maps to an allocation that is suitable for {@code cudaIpcGetMemHandle()}, 0 otherwise"
),
"POINTER_ATTRIBUTE_RANGE_START_ADDR".enum("Starting address for this requested pointer"),
"POINTER_ATTRIBUTE_RANGE_SIZE".enum("Size of the address range for this requested pointer"),
"POINTER_ATTRIBUTE_MAPPED".enum("1 if this pointer is in a valid address range that is mapped to a backing allocation, 0 otherwise"),
"POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES".enum("Bitmask of allowed {@code CUmemAllocationHandleType} for this allocation"),
"POINTER_ATTRIBUTE_IS_GPU_DIRECT_RDMA_CAPABLE".enum("1 if the memory this pointer is referencing can be used with the GPUDirect RDMA API"),
"POINTER_ATTRIBUTE_ACCESS_FLAGS".enum(
"Returns the access flags the device associated with the current context has on the corresponding memory referenced by the pointer given"
),
"POINTER_ATTRIBUTE_MEMPOOL_HANDLE".enum(
"Returns the {@code mempoo}l handle for the allocation if it was allocated from a {@code mempool}. Otherwise returns #NULL."
)
)
EnumConstant(
"Function properties. ({@code CUfunction_attribute})",
"FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK".enum(
"""
The maximum number of threads per block, beyond which a launch of the function would fail. This number depends on both the function and the device
on which the function is currently loaded.
""",
0
),
"FUNC_ATTRIBUTE_SHARED_SIZE_BYTES".enum(
"""
The size in bytes of statically-allocated shared memory required by this function. This does not include dynamically-allocated shared memory
requested by the user at runtime.
""",
1
),
"FUNC_ATTRIBUTE_CONST_SIZE_BYTES".enum("The size in bytes of user-allocated constant memory required by this function.", 2),
"FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES".enum("The size in bytes of local memory used by each thread of this function.", 3),
"FUNC_ATTRIBUTE_NUM_REGS".enum("The number of registers used by each thread of this function.", 4),
"FUNC_ATTRIBUTE_PTX_VERSION".enum(
"""
The PTX virtual architecture version for which the function was compiled.
This value is the major PTX {@code version * 10 + the minor PTX version}, so a PTX version 1.3 function would return the value 13. Note that this
may return the undefined value of 0 for cubins compiled prior to CUDA 3.0.
""",
5
),
"FUNC_ATTRIBUTE_BINARY_VERSION".enum(
"""
The binary architecture version for which the function was compiled.
This value is the {@code major binary version * 10 + the minor binary version}, so a binary version 1.3 function would return the value 13. Note
that this will return a value of 10 for legacy cubins that do not have a properly-encoded binary architecture version.
""",
6
),
"FUNC_ATTRIBUTE_CACHE_MODE_CA".enum(
"The attribute to indicate whether the function has been compiled with user specified option {@code \"-Xptxas --dlcm=ca\"} set.",
7
),
"FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES".enum(
"""
The maximum size in bytes of dynamically-allocated shared memory that can be used by this function.
If the user-specified dynamic shared memory size is larger than this value, the launch will fail.
""",
8
),
"FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT".enum(
"""
On devices where the L1 cache and shared memory use the same hardware resources, this sets the shared memory carveout preference, in percent of the total shared memory. Refer to #DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_MULTIPROCESSOR.
This is only a hint, and the driver can choose a different ratio if required to execute the function.
""",
9
)
)
EnumConstant(
"Function cache configurations. ({@code CUfunc_cache})",
"FUNC_CACHE_PREFER_NONE".enum("no preference for shared memory or L1 (default)", 0x00),
"FUNC_CACHE_PREFER_SHARED".enum("prefer larger shared memory and smaller L1 cache", 0x01),
"FUNC_CACHE_PREFER_L1".enum("prefer larger L1 cache and smaller shared memory", 0x02),
"FUNC_CACHE_PREFER_EQUAL".enum("prefer equal sized L1 cache and shared memory", 0x03)
)
EnumConstant(
"Shared memory configurations. ({@code CUsharedconfig})",
"SHARED_MEM_CONFIG_DEFAULT_BANK_SIZE".enum("set default shared memory bank size", 0x00),
"SHARED_MEM_CONFIG_FOUR_BYTE_BANK_SIZE".enum("set shared memory bank width to four bytes", 0x01),
"SHARED_MEM_CONFIG_EIGHT_BYTE_BANK_SIZE".enum("set shared memory bank width to eight bytes", 0x02)
)
EnumConstant(
"""
Shared memory carveout configurations. ({@code CUshared_carveout})
These may be passed to #FuncSetAttribute().
""",
"SHAREDMEM_CARVEOUT_DEFAULT".enum("no preference for shared memory or L1 (default)", -1),
"SHAREDMEM_CARVEOUT_MAX_SHARED".enum("prefer maximum available shared memory, minimum L1 cache", 100),
"SHAREDMEM_CARVEOUT_MAX_L1".enum("prefer maximum available L1 cache, minimum shared memory", 0)
)
EnumConstant(
"Memory types. ({@code CUmemorytype})",
"MEMORYTYPE_HOST".enum("Host memory", 0x01),
"MEMORYTYPE_DEVICE".enum("Device memory", 0x02),
"MEMORYTYPE_ARRAY".enum("Array memory", 0x03),
"MEMORYTYPE_UNIFIED".enum("Unified device or host memory", 0x04)
)
EnumConstant(
"Compute Modes. ({@code CUcomputemode})",
"COMPUTEMODE_DEFAULT".enum("Default compute mode (Multiple contexts allowed per device)", 0),
"COMPUTEMODE_PROHIBITED".enum("Compute-prohibited mode (No contexts can be created on this device at this time)", 2),
"COMPUTEMODE_EXCLUSIVE_PROCESS".enum(
"Compute-exclusive-process mode (Only one context used by a single process can be present on this device at a time)",
3
)
)
EnumConstant(
"Memory advise values. ({@code CUmem_advise})",
"MEM_ADVISE_SET_READ_MOSTLY".enum("Data will mostly be read and only occassionally be written to", 1),
"MEM_ADVISE_UNSET_READ_MOSTLY".enum("Undo the effect of #MEM_ADVISE_SET_READ_MOSTLY", 2),
"MEM_ADVISE_SET_PREFERRED_LOCATION".enum("Set the preferred location for the data as the specified device", 3),
"MEM_ADVISE_UNSET_PREFERRED_LOCATION".enum("Clear the preferred location for the data", 4),
"MEM_ADVISE_SET_ACCESSED_BY".enum("Data will be accessed by the specified device, so prevent page faults as much as possible", 5),
"MEM_ADVISE_UNSET_ACCESSED_BY".enum("Let the Unified Memory subsystem decide on the page faulting policy for the specified device", 6)
)
EnumConstant(
"({@code CUmem_range_attribute})",
"MEM_RANGE_ATTRIBUTE_READ_MOSTLY".enum("Whether the range will mostly be read and only occassionally be written to", 1),
"MEM_RANGE_ATTRIBUTE_PREFERRED_LOCATION".enum("The preferred location of the range", 2),
"MEM_RANGE_ATTRIBUTE_ACCESSED_BY".enum("Memory range has #MEM_ADVISE_SET_ACCESSED_BY set for specified device", 3),
"MEM_RANGE_ATTRIBUTE_LAST_PREFETCH_LOCATION".enum("The last location to which the range was prefetched", 4)
)
EnumConstant(
"Online compiler and linker options. ({@code CUjit_option})",
"JIT_MAX_REGISTERS".enum(
"""
Max number of registers that a thread may use.
Option type: {@code unsigned int}. Applies to: compiler only
""",
0
),
"JIT_THREADS_PER_BLOCK".enum(
"""
IN: Specifies minimum number of threads per block to target compilation for
OUT: Returns the number of threads the compiler actually targeted.
This restricts the resource utilization fo the compiler (e.g. max registers) such that a block with the given number of threads should be able to
launch based on register limitations. Note, this option does not currently take into account any other resource limitations, such as shared memory
utilization.
Cannot be combined with #JIT_TARGET. Option type: {@code unsigned int}. Applies to: compiler only
"""
),
"JIT_WALL_TIME".enum(
"""
Overwrites the option value with the total wall clock time, in milliseconds, spent in the compiler and linker.
Option type: {@code float}. Applies to: compiler and linker
"""
),
"JIT_INFO_LOG_BUFFER".enum(
"""
Pointer to a buffer in which to print any log messages that are informational in nature (the buffer size is specified via option
#JIT_INFO_LOG_BUFFER_SIZE_BYTES).
Option type: {@code char *}. Applies to: compiler and linker
"""
),
"JIT_INFO_LOG_BUFFER_SIZE_BYTES".enum(
"""
IN: Log buffer size in bytes. Log messages will be capped at this size (including null terminator).
OUT: Amount of log buffer filled with messages.
Option type: {@code unsigned int}. Applies to: compiler and linker
"""
),
"JIT_ERROR_LOG_BUFFER".enum(
"""
Pointer to a buffer in which to print any log messages that reflect errors (the buffer size is specified via option
#JIT_ERROR_LOG_BUFFER_SIZE_BYTES).
Option type: {@code char *}. Applies to: compiler and linker
"""
),
"JIT_ERROR_LOG_BUFFER_SIZE_BYTES".enum(
"""
IN: Log buffer size in bytes. Log messages will be capped at this size (including null terminator).
OUT: Amount of log buffer filled with messages.
Option type: {@code unsigned int}. Applies to: compiler and linker
"""
),
"JIT_OPTIMIZATION_LEVEL".enum(
"""
Level of optimizations to apply to generated code (0 - 4), with 4 being the default and highest level of optimizations.
Option type: {@code unsigned int}. Applies to: compiler only
"""
),
"JIT_TARGET_FROM_CUCONTEXT".enum(
"""
No option value required. Determines the target based on the current attached context (default).
Option type: No option value needed. Applies to: compiler and linker
"""
),
"JIT_TARGET".enum(
"""
Target is chosen based on supplied {@code CUjit_target}. Cannot be combined with #JIT_THREADS_PER_BLOCK.
Option type: {@code unsigned int} for enumerated type {@code CUjit_target}. Applies to: compiler and linker
"""
),
"JIT_FALLBACK_STRATEGY".enum(
"""
Specifies choice of fallback strategy if matching cubin is not found.
Choice is based on supplied {@code CUjit_fallback}. This option cannot be used with {@code cuLink*} APIs as the linker requires exact matches.
Option type: {@code unsigned int} for enumerated type {@code CUjit_fallback}. Applies to: compiler only
"""
),
"JIT_GENERATE_DEBUG_INFO".enum(
"""
Specifies whether to create debug information in output (-g) (0: false, default).
Option type: {@code int}. Applies to: compiler and linker
"""
),
"JIT_LOG_VERBOSE".enum(
"""
Generate verbose log messages (0: false, default).
Option type: {@code int}. Applies to: compiler and linker
"""
),
"JIT_GENERATE_LINE_INFO".enum(
"""
Generate line number information (-lineinfo) (0: false, default).
Option type: {@code int}. Applies to: compiler only
"""
),
"JIT_CACHE_MODE".enum(
"""
Specifies whether to enable caching explicitly (-dlcm). Choice is based on supplied {@code CUjit_cacheMode_enum}.
Option type: {@code unsigned int} for enumerated type {@code CUjit_cacheMode_enum}. Applies to: compiler only
"""
),
"JIT_NEW_SM3X_OPT".enum("Used for internal purposes only, in this version of CUDA."),
"JIT_FAST_COMPILE".enum("Used for internal purposes only, in this version of CUDA."),
"JIT_GLOBAL_SYMBOL_NAMES".enum(
"""
Array of device symbol names that will be relocated to the corresponing host addresses stored in #JIT_GLOBAL_SYMBOL_ADDRESSES.
Must contain #JIT_GLOBAL_SYMBOL_COUNT entries. When loding a device module, driver will relocate all encountered unresolved symbols to the host
addresses. It is only allowed to register symbols that correspond to unresolved global variables. It is illegal to register the same device symbol
at multiple addresses.
Option type: {@code const char **}. Applies to: dynamic linker only
"""
),
"JIT_GLOBAL_SYMBOL_ADDRESSES".enum(
"""
Array of host addresses that will be used to relocate corresponding device symbols stored in #JIT_GLOBAL_SYMBOL_NAMES.
Must contain #JIT_GLOBAL_SYMBOL_COUNT entries.
Option type: {@code void **}. Applies to: dynamic linker only
"""
),
"JIT_GLOBAL_SYMBOL_COUNT".enum(
"""
Number of entries in #JIT_GLOBAL_SYMBOL_NAMES and #JIT_GLOBAL_SYMBOL_ADDRESSES arrays.
Option type: {@code unsigned int}. Applies to: dynamic linker only
"""
),
"JIT_LTO".enum(
"""
Enable link-time optimization (-dlto) for device code (0: false, default)
Option type: {@code int}. Applies to: compiler and linker
"""
),
"JIT_FTZ".enum(
"""
Control single-precision denormals (-ftz) support (0: false, default).
${ul(
"1 : flushes denormal values to zero",
"0 : preserves denormal values"
)}
Option type: {@code int}. Applies to: link-time optimization specified with #JIT_LTO
"""
),
"JIT_PREC_DIV".enum(
"""
Control single-precision floating-point division and reciprocals (-prec-div) support (1: true, default).
${ul(
"1 : Enables the IEEE round-to-nearest mode",
"0 : Enables the fast approximation mode"
)}
Option type: {@code int}. Applies to: link-time optimization specified with #JIT_LTO
"""
),
"JIT_PREC_SQRT".enum(
"""
Control single-precision floating-point square root (-prec-sqrt) support (1: true, default).
${ul(
"1 : Enables the IEEE round-to-nearest mode",
"0 : Enables the fast approximation mode"
)}
Option type: {@code int}. Applies to: link-time optimization specified with #JIT_LTO
"""
),
"JIT_FMA".enum(
"""
Enable/Disable the contraction of floating-point multiplies and adds/subtracts into floating-point multiply-add (-fma) operations (1: Enable,
default; 0: Disable).
Option type: {@code int}. Applies to: link-time optimization specified with #JIT_LTO
"""
),
"JIT_NUM_OPTIONS".enum(
"""
Enable/Disable the contraction of floating-point multiplies and adds/subtracts into floating-point multiply-add (-fma) operations (1: Enable,
default; 0: Disable).
Option type: {@code int}. Applies to: link-time optimization specified with #JIT_LTO
"""
)
)
EnumConstant(
"Online compilation targets. ({@code CUjit_target})",
"TARGET_COMPUTE_20".enum("Compute device class 2.0", "20"),
"TARGET_COMPUTE_21".enum("Compute device class 2.1"),
"TARGET_COMPUTE_30".enum("Compute device class 3.0", "30"),
"TARGET_COMPUTE_32".enum("Compute device class 3.2", "32"),
"TARGET_COMPUTE_35".enum("Compute device class 3.5", "35"),
"TARGET_COMPUTE_37".enum("Compute device class 3.7", "37"),
"TARGET_COMPUTE_50".enum("Compute device class 5.0", "50"),
"TARGET_COMPUTE_52".enum("Compute device class 5.2", "52"),
"TARGET_COMPUTE_53".enum("Compute device class 5.3"),
"TARGET_COMPUTE_60".enum("Compute device class 6.0.", "60"),
"TARGET_COMPUTE_61".enum("Compute device class 6.1."),
"TARGET_COMPUTE_62".enum("Compute device class 6.2."),
"TARGET_COMPUTE_70".enum("Compute device class 7.0.", "70"),
"TARGET_COMPUTE_72".enum("Compute device class 7.2.", "72"),
"TARGET_COMPUTE_75".enum("Compute device class 7.5.", "75"),
"TARGET_COMPUTE_80".enum("Compute device class 8.0.", "80"),
"TARGET_COMPUTE_86".enum("Compute device class 8.6.", "86")
)
EnumConstant(
"Cubin matching fallback strategies. ({@code CUjit_fallback})",
"PREFER_PTX".enum("Prefer to compile ptx if exact binary match not found", 0),
"PREFER_BINARY".enum("Prefer to fall back to compatible binary code if exact match not found")
)
EnumConstant(
"Caching modes for {@code dlcm}. ({@code CUjit_cacheMode})",
"JIT_CACHE_OPTION_NONE".enum("Compile with no -dlcm flag specified", 0),
"JIT_CACHE_OPTION_CG".enum("Compile with L1 cache disabled"),
"JIT_CACHE_OPTION_CA".enum("Compile with L1 cache enabled")
)
EnumConstant(
"Device code formats. ({@code CUjitInputType})",
"JIT_INPUT_CUBIN".enum(
"""
Compiled device-class-specific device code
Applicable options: none
""",
"0"
),
"JIT_INPUT_PTX".enum(
"""
PTX source code.
Applicable options: PTX compiler options
"""
),
"JIT_INPUT_FATBINARY".enum(
"""
Bundle of multiple cubins and/or PTX of some device code.
Applicable options: PTX compiler options, #JIT_FALLBACK_STRATEGY
"""
),
"JIT_INPUT_OBJECT".enum(
"""
Host object with embedded device code.
Applicable options: PTX compiler options, #JIT_FALLBACK_STRATEGY
"""
),
"JIT_INPUT_LIBRARY".enum(
"""
Archive of host objects with embedded device code.
Applicable options: PTX compiler options, #JIT_FALLBACK_STRATEGY
"""
),
"JIT_INPUT_NVVM".enum(
"""
High-level intermediate code for link-time optimization.
Applicable options: NVVM compiler options, PTX compiler options
"""
),
)
EnumConstant(
"Flags to register a graphics resource. ({@code CUgraphicsRegisterFlags})",
"GRAPHICS_REGISTER_FLAGS_NONE".enum("", 0x00),
"GRAPHICS_REGISTER_FLAGS_READ_ONLY".enum("", 0x01),
"GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD".enum("", 0x02),
"GRAPHICS_REGISTER_FLAGS_SURFACE_LDST".enum("", 0x04),
"GRAPHICS_REGISTER_FLAGS_TEXTURE_GATHER".enum("", 0x08)
)
EnumConstant(
"Flags for mapping and unmapping interop resources. ({@code CUgraphicsMapResourceFlags})",
"GRAPHICS_MAP_RESOURCE_FLAGS_NONE".enum("", 0x00),
"GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY".enum("", 0x01),
"GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD".enum("", 0x02)
)
EnumConstant(
"Array indices for cube faces. ({@code CUarray_cubemap_face})",
"CUBEMAP_FACE_POSITIVE_X".enum("Positive X face of cubemap", 0x00),
"CUBEMAP_FACE_NEGATIVE_X".enum("Negative X face of cubemap", 0x01),
"CUBEMAP_FACE_POSITIVE_Y".enum("Positive Y face of cubemap", 0x02),
"CUBEMAP_FACE_NEGATIVE_Y".enum("Negative Y face of cubemap", 0x03),
"CUBEMAP_FACE_POSITIVE_Z".enum("Positive Z face of cubemap", 0x04),
"CUBEMAP_FACE_NEGATIVE_Z".enum("Negative Z face of cubemap", 0x05)
)
EnumConstant(
"Limits. ({@code CUlimit})",
"LIMIT_STACK_SIZE".enum("GPU thread stack size", "0x00"),
"LIMIT_PRINTF_FIFO_SIZE".enum("GPU printf FIFO size", "0x01"),
"LIMIT_MALLOC_HEAP_SIZE".enum("GPU malloc heap size", "0x02"),
"LIMIT_DEV_RUNTIME_SYNC_DEPTH".enum("GPU device runtime launch synchronize depth", "0x03"),
"LIMIT_DEV_RUNTIME_PENDING_LAUNCH_COUNT".enum("GPU device runtime pending launch count", "0x04"),
"LIMIT_MAX_L2_FETCH_GRANULARITY".enum(
"A value between 0 and 128 that indicates the maximum fetch granularity of L2 (in Bytes). This is a hint",
"0x05"
),
"LIMIT_PERSISTING_L2_CACHE_SIZE".enum("A size in bytes for L2 persisting lines cache size", "0x06")
)
EnumConstant(
"Resource types. ({@code CUresourcetype})",
"RESOURCE_TYPE_ARRAY".enum("Array resoure", 0x00),
"RESOURCE_TYPE_MIPMAPPED_ARRAY".enum("Mipmapped array resource", 0x01),
"RESOURCE_TYPE_LINEAR".enum("Linear resource", 0x02),
"RESOURCE_TYPE_PITCH2D".enum("Pitch 2D resource", 0x03)
)
EnumConstant(
"Specifies performance hint with ##CUaccessPolicyWindow for {@code hitProp} and {@code missProp} members. ({@code CUaccessProperty})",
"ACCESS_PROPERTY_NORMAL".enum("Normal cache persistence.", "0"),
"ACCESS_PROPERTY_STREAMING".enum("Streaming access is less likely to persit from cache."),
"ACCESS_PROPERTY_PERSISTING".enum("Persisting access is more likely to persist in cache.")
)
EnumConstant(
"Graph node types. ({@code CUgraphNodeType})",
"GRAPH_NODE_TYPE_KERNEL".enum("GPU kernel node", "0"),
"GRAPH_NODE_TYPE_MEMCPY".enum("Memcpy node"),
"GRAPH_NODE_TYPE_MEMSET".enum("Memset node"),
"GRAPH_NODE_TYPE_HOST".enum("Host (executable) node"),
"GRAPH_NODE_TYPE_GRAPH".enum("Node which executes an embedded graph"),
"GRAPH_NODE_TYPE_EMPTY".enum("Empty (no-op) node"),
"GRAPH_NODE_TYPE_WAIT_EVENT".enum("External event wait node"),
"GRAPH_NODE_TYPE_EVENT_RECORD".enum("External event record node"),
"GRAPH_NODE_TYPE_EXT_SEMAS_SIGNAL".enum("External semaphore signal node"),
"GRAPH_NODE_TYPE_EXT_SEMAS_WAIT".enum("External semaphore wait node"),
"GRAPH_NODE_TYPE_MEM_ALLOC".enum("Memory Allocation Node"),
"GRAPH_NODE_TYPE_MEM_FREE".enum("Memory Free Node")
)
EnumConstant(
"{@code CUsynchronizationPolicy}",
"SYNC_POLICY_AUTO".enum("", "1"),
"SYNC_POLICY_SPIN".enum,
"SYNC_POLICY_YIELD".enum,
"SYNC_POLICY_BLOCKING_SYNC".enum
)
EnumConstant(
"Graph kernel node Attributes ({@code CUkernelNodeAttrID})",
"KERNEL_NODE_ATTRIBUTE_ACCESS_POLICY_WINDOW".enum("Identifier for ##CUkernelNodeAttrValue{@code {@code accessPolicyWindow}}.", "1"),
"KERNEL_NODE_ATTRIBUTE_COOPERATIVE".enum("Allows a kernel node to be cooperative (see #LaunchCooperativeKernel()).")
)
EnumConstant(
"Possible stream capture statuses returned by #StreamIsCapturing(). ({@code CUstreamCaptureStatus})",
"STREAM_CAPTURE_STATUS_NONE".enum("Stream is not capturing", 0),
"STREAM_CAPTURE_STATUS_ACTIVE".enum("Stream is actively capturing", 1),
"STREAM_CAPTURE_STATUS_INVALIDATED".enum("Stream is part of a capture sequence that has been invalidated, but not terminated", 2)
)
EnumConstant(
"""
Possible modes for stream capture thread interactions. ({@code CUstreamCaptureMode})
For more details see #StreamBeginCapture() and #ThreadExchangeStreamCaptureMode()
""",
"STREAM_CAPTURE_MODE_GLOBAL".enum("", "0"),
"STREAM_CAPTURE_MODE_THREAD_LOCAL".enum,
"STREAM_CAPTURE_MODE_RELAXED".enum
)
EnumConstant(
"Stream Attributes ({@code CUstreamAttrID})",
"STREAM_ATTRIBUTE_ACCESS_POLICY_WINDOW".enum("Identifier for ##CUstreamAttrValue{@code {@code accessPolicyWindow}}.", "1"),
"STREAM_ATTRIBUTE_SYNCHRONIZATION_POLICY".enum("{@code CUsynchronizationPolicy} for work queued up in this stream", "3")
)
EnumConstant(
"Flags to specify search options. For more details see #GetProcAddress(). ({@code CUdriverProcAddress_flags})",
"GET_PROC_ADDRESS_DEFAULT".enum("Default search mode for driver symbols.", "0"),
"GET_PROC_ADDRESS_LEGACY_STREAM".enum("Search for legacy versions of driver symbols.", "1 << 0"),
"GET_PROC_ADDRESS_PER_THREAD_DEFAULT_STREAM".enum("Search for per-thread versions of driver symbols.", "1 << 1")
)
EnumConstant(
"""
Execution Affinity Types
({@code CUexecAffinityType})
""",
"EXEC_AFFINITY_TYPE_SM_COUNT".enum("Create a context with limited SMs.", "0"),
"EXEC_AFFINITY_TYPE_MAX".enum
)
EnumConstant(
"Error codes. ({@code CUresult})",
"CUDA_SUCCESS".enum(
"""
The API call returned with no errors.
In the case of query calls, this also means that the operation being queried is complete (see #EventQuery() and #StreamQuery()).
""",
"0"
),
"CUDA_ERROR_INVALID_VALUE".enum(
"This indicates that one or more of the parameters passed to the API call is not within an acceptable range of values."
),
"CUDA_ERROR_OUT_OF_MEMORY".enum("The API call failed because it was unable to allocate enough memory to perform the requested operation."),
"CUDA_ERROR_NOT_INITIALIZED".enum("This indicates that the CUDA driver has not been initialized with #Init() or that initialization has failed."),
"CUDA_ERROR_DEINITIALIZED".enum("This indicates that the CUDA driver is in the process of shutting down."),
"CUDA_ERROR_PROFILER_DISABLED".enum(
"""
This indicates profiler is not initialized for this run. This can happen when the application is running with external profiling tools like visual
profiler.
"""
),
"CUDA_ERROR_PROFILER_NOT_INITIALIZED".enum(
"""
Deprecated: This error return is deprecated as of CUDA 5.0. It is no longer an error to attempt to enable/disable the profiling via
#ProfilerStart() or #ProfilerStop() without initialization.
"""
),
"CUDA_ERROR_PROFILER_ALREADY_STARTED".enum(
"""
Deprecated: This error return is deprecated as of CUDA 5.0. It is no longer an error to call #ProfilerStart() when profiling is already enabled.
"""
),
"CUDA_ERROR_PROFILER_ALREADY_STOPPED".enum(
"""
Deprecated: This error return is deprecated as of CUDA 5.0. It is no longer an error to call #ProfilerStop() when profiling is already disabled.
"""
),
"CUDA_ERROR_STUB_LIBRARY".enum(
"""
This indicates that the CUDA driver that the application has loaded is a stub library. Applications that run with the stub rather than a real
driver loaded will result in CUDA API returning this error.
""",
"34"
),
"CUDA_ERROR_NO_DEVICE".enum("This indicates that no CUDA-capable devices were detected by the installed CUDA driver.", "100"),
"CUDA_ERROR_INVALID_DEVICE".enum(
"""
This indicates that the device ordinal supplied by the user does not correspond to a valid CUDA device or that the action requested is invalid for
the specified device.
"""
),
"CUDA_ERROR_DEVICE_NOT_LICENSED".enum("This error indicates that the Grid license is not applied."),
"CUDA_ERROR_INVALID_IMAGE".enum("This indicates that the device kernel image is invalid. This can also indicate an invalid CUDA module.", "200"),
"CUDA_ERROR_INVALID_CONTEXT".enum(
"""
This most frequently indicates that there is no context bound to the current thread. This can also be returned if the context passed to an API call
is not a valid handle (such as a context that has had #CtxDestroy() invoked on it). This can also be returned if a user mixes different API
versions (i.e. 3010 context with 3020 API calls). See #CtxGetApiVersion() for more details.
"""
),
"CUDA_ERROR_CONTEXT_ALREADY_CURRENT".enum(
"""
This indicated that the context being supplied as a parameter to the API call was already the active context.Deprecated: This error return is
deprecated as of CUDA 3.2. It is no longer an error to attempt to push the active context via #CtxPushCurrent().
"""
),
"CUDA_ERROR_MAP_FAILED".enum("This indicates that a map or register operation has failed.", "205"),
"CUDA_ERROR_UNMAP_FAILED".enum("This indicates that an unmap or unregister operation has failed."),
"CUDA_ERROR_ARRAY_IS_MAPPED".enum("This indicates that the specified array is currently mapped and thus cannot be destroyed."),
"CUDA_ERROR_ALREADY_MAPPED".enum("This indicates that the resource is already mapped."),
"CUDA_ERROR_NO_BINARY_FOR_GPU".enum(
"""
This indicates that there is no kernel image available that is suitable for the device. This can occur when a user specifies code generation
options for a particular CUDA source file that do not include the corresponding device configuration.
"""
),
"CUDA_ERROR_ALREADY_ACQUIRED".enum("This indicates that a resource has already been acquired."),
"CUDA_ERROR_NOT_MAPPED".enum("This indicates that a resource is not mapped."),
"CUDA_ERROR_NOT_MAPPED_AS_ARRAY".enum("This indicates that a mapped resource is not available for access as an array."),
"CUDA_ERROR_NOT_MAPPED_AS_POINTER".enum("This indicates that a mapped resource is not available for access as a pointer."),
"CUDA_ERROR_ECC_UNCORRECTABLE".enum("This indicates that an uncorrectable ECC error was detected during execution."),
"CUDA_ERROR_UNSUPPORTED_LIMIT".enum("This indicates that the {@code CUlimit} passed to the API call is not supported by the active device."),
"CUDA_ERROR_CONTEXT_ALREADY_IN_USE".enum(
"""
This indicates that the {@code CUcontext} passed to the API call can only be bound to a single CPU thread at a time but is already bound to a CPU thread.
"""
),
"CUDA_ERROR_PEER_ACCESS_UNSUPPORTED".enum("This indicates that peer access is not supported across the given devices."),
"CUDA_ERROR_INVALID_PTX".enum("This indicates that a PTX JIT compilation failed."),
"CUDA_ERROR_INVALID_GRAPHICS_CONTEXT".enum("This indicates an error with OpenGL or DirectX context."),
"CUDA_ERROR_NVLINK_UNCORRECTABLE".enum("This indicates that an uncorrectable NVLink error was detected during the execution."),
"CUDA_ERROR_JIT_COMPILER_NOT_FOUND".enum("This indicates that the PTX JIT compiler library was not found."),
"CUDA_ERROR_UNSUPPORTED_PTX_VERSION".enum("This indicates that the provided PTX was compiled with an unsupported toolchain."),
"CUDA_ERROR_JIT_COMPILATION_DISABLED".enum("This indicates that the PTX JIT compilation was disabled."),
"CUDA_ERROR_UNSUPPORTED_EXEC_AFFINITY".enum(
"This indicates that the {@code CUexecAffinityType} passed to the API call is not supported by the active device."
),
"CUDA_ERROR_INVALID_SOURCE".enum(
"This indicates that the device kernel source is invalid. This includes compilation/linker errors encountered in device code or user error.",
"300"
),
"CUDA_ERROR_FILE_NOT_FOUND".enum("This indicates that the file specified was not found."),
"CUDA_ERROR_SHARED_OBJECT_SYMBOL_NOT_FOUND".enum("This indicates that a link to a shared object failed to resolve."),
"CUDA_ERROR_SHARED_OBJECT_INIT_FAILED".enum("This indicates that initialization of a shared object failed."),
"CUDA_ERROR_OPERATING_SYSTEM".enum("This indicates that an OS call failed."),
"CUDA_ERROR_INVALID_HANDLE".enum(
"""
This indicates that a resource handle passed to the API call was not valid. Resource handles are opaque types like {@code CUstream} and
{@code CUevent}.
""",
"400"
),
"CUDA_ERROR_ILLEGAL_STATE".enum("This indicates that a resource required by the API call is not in a valid state to perform the requested operation."),
"CUDA_ERROR_NOT_FOUND".enum(
"""
This indicates that a named symbol was not found. Examples of symbols are global/constant variable names, driver function names, texture names, and
surface names.
""",
"500"
),
"CUDA_ERROR_NOT_READY".enum(
"""
This indicates that asynchronous operations issued previously have not completed yet. This result is not actually an error, but must be indicated
differently than #CUDA_SUCCESS (which indicates completion). Calls that may return this value include #EventQuery() and #StreamQuery().
""",
"600"
),
"CUDA_ERROR_ILLEGAL_ADDRESS".enum(
"""
While executing a kernel, the device encountered a load or store instruction on an invalid memory address. This leaves the process in an
inconsistent state and any further CUDA work will return the same error. To continue using CUDA, the process must be terminated and relaunched.
""",
"700"
),
"CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES".enum(
"""
This indicates that a launch did not occur because it did not have appropriate resources. This error usually indicates that the user has attempted
to pass too many arguments to the device kernel, or the kernel launch specifies too many threads for the kernel's register count. Passing arguments
of the wrong size (i.e. a 64-bit pointer when a 32-bit int is expected) is equivalent to passing too many arguments and can also result in this
error.
"""
),
"CUDA_ERROR_LAUNCH_TIMEOUT".enum(
"""
This indicates that the device kernel took too long to execute. This can only occur if timeouts are enabled - see the device attribute
#DEVICE_ATTRIBUTE_KERNEL_EXEC_TIMEOUT for more information. This leaves the process in an inconsistent state and any further CUDA work will
return the same error. To continue using CUDA, the process must be terminated and relaunched.
"""
),
"CUDA_ERROR_LAUNCH_INCOMPATIBLE_TEXTURING".enum("This error indicates a kernel launch that uses an incompatible texturing mode."),
"CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED".enum(
"""
This error indicates that a call to #CtxEnablePeerAccess() is trying to re-enable peer access to a context which has already had peer access to
it enabled.
"""
),
"CUDA_ERROR_PEER_ACCESS_NOT_ENABLED".enum(
"""
This error indicates that #CtxDisablePeerAccess() is trying to disable peer access which has not been enabled yet via #CtxEnablePeerAccess().
"""
),
"CUDA_ERROR_PRIMARY_CONTEXT_ACTIVE".enum(
"This error indicates that the primary context for the specified device has already been initialized.",
"708"
),
"CUDA_ERROR_CONTEXT_IS_DESTROYED".enum(
"""
This error indicates that the context current to the calling thread has been destroyed using #CtxDestroy(), or is a primary context which has not
yet been initialized.
"""
),
"CUDA_ERROR_ASSERT".enum(
"""
A device-side assert triggered during kernel execution. The context cannot be used anymore, and must be destroyed. All existing device memory
allocations from this context are invalid and must be reconstructed if the program is to continue using CUDA.
"""
),
"CUDA_ERROR_TOO_MANY_PEERS".enum(
"""
This error indicates that the hardware resources required to enable peer access have been exhausted for one or more of the devices passed to
#CtxEnablePeerAccess().
"""
),
"CUDA_ERROR_HOST_MEMORY_ALREADY_REGISTERED".enum(
"This error indicates that the memory range passed to #MemHostRegister() has already been registered."
),
"CUDA_ERROR_HOST_MEMORY_NOT_REGISTERED".enum(
"This error indicates that the pointer passed to #MemHostUnregister() does not correspond to any currently registered memory region."
),
"CUDA_ERROR_HARDWARE_STACK_ERROR".enum(
"""
While executing a kernel, the device encountered a stack error. This can be due to stack corruption or exceeding the stack size limit. This leaves
the process in an inconsistent state and any further CUDA work will return the same error. To continue using CUDA, the process must be terminated
and relaunched.
"""
),
"CUDA_ERROR_ILLEGAL_INSTRUCTION".enum(
"""
While executing a kernel, the device encountered an illegal instruction. This leaves the process in an inconsistent state and any further CUDA work
will return the same error. To continue using CUDA, the process must be terminated and relaunched.
"""
),
"CUDA_ERROR_MISALIGNED_ADDRESS".enum(
"""
While executing a kernel, the device encountered a load or store instruction on a memory address which is not aligned. This leaves the process in
an inconsistent state and any further CUDA work will return the same error. To continue using CUDA, the process must be terminated and relaunched.
"""
),
"CUDA_ERROR_INVALID_ADDRESS_SPACE".enum(
"""
While executing a kernel, the device encountered an instruction which can only operate on memory locations in certain address spaces (global,
shared, or local), but was supplied a memory address not belonging to an allowed address space. This leaves the process in an inconsistent state
and any further CUDA work will return the same error. To continue using CUDA, the process must be terminated and relaunched.
"""
),
"CUDA_ERROR_INVALID_PC".enum(
"""
While executing a kernel, the device program counter wrapped its address space. This leaves the process in an inconsistent state and any further
CUDA work will return the same error. To continue using CUDA, the process must be terminated and relaunched.
"""
),
"CUDA_ERROR_LAUNCH_FAILED".enum(
"""
An exception occurred on the device while executing a kernel. Common causes include dereferencing an invalid device pointer and accessing out of
bounds shared memory. Less common cases can be system specific - more information about these cases can be found in the system specific user guide.
This leaves the process in an inconsistent state and any further CUDA work will return the same error. To continue using CUDA, the process must be
terminated and relaunched.
"""
),
"CUDA_ERROR_COOPERATIVE_LAUNCH_TOO_LARGE".enum(
"""
This error indicates that the number of blocks launched per grid for a kernel that was launched via either #LaunchCooperativeKernel() or
#LaunchCooperativeKernelMultiDevice() exceeds the maximum number of blocks as allowed by #OccupancyMaxActiveBlocksPerMultiprocessor() or
#OccupancyMaxActiveBlocksPerMultiprocessorWithFlags() times the number of multiprocessors as specified by the device attribute
#DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT.
"""
),
"CUDA_ERROR_NOT_PERMITTED".enum("This error indicates that the attempted operation is not permitted.", "800"),
"CUDA_ERROR_NOT_SUPPORTED".enum("This error indicates that the attempted operation is not supported on the current system or device."),
"CUDA_ERROR_SYSTEM_NOT_READY".enum(
"""
This error indicates that the system is not yet ready to start any CUDA work. To continue using CUDA, verify the system configuration is in a valid
state and all required driver daemons are actively running. More information about this error can be found in the system specific user guide.
"""
),
"CUDA_ERROR_SYSTEM_DRIVER_MISMATCH".enum(
"""
This error indicates that there is a mismatch between the versions of the display driver and the CUDA driver. Refer to the compatibility
documentation for supported versions.
"""
),
"CUDA_ERROR_COMPAT_NOT_SUPPORTED_ON_DEVICE".enum(
"""
This error indicates that the system was upgraded to run with forward compatibility but the visible hardware detected by CUDA does not support this
configuration. Refer to the compatibility documentation for the supported hardware matrix or ensure that only supported hardware is visible during
initialization via the {@code CUDA_VISIBLE_DEVICES} environment variable.
"""
),
"CUDA_ERROR_MPS_CONNECTION_FAILED".enum("This error indicates that the MPS client failed to connect to the MPS control daemon or the MPS server."),
"CUDA_ERROR_MPS_RPC_FAILURE".enum("This error indicates that the remote procedural call between the MPS server and the MPS client failed."),
"CUDA_ERROR_MPS_SERVER_NOT_READY".enum(
"""
This error indicates that the MPS server is not ready to accept new MPS client requests. This error can be returned when the MPS server is in the
process of recovering from a fatal failure.
"""
),
"CUDA_ERROR_MPS_MAX_CLIENTS_REACHED".enum("This error indicates that the hardware resources required to create MPS client have been exhausted."),
"CUDA_ERROR_MPS_MAX_CONNECTIONS_REACHED".enum(
"This error indicates the the hardware resources required to support device connections have been exhausted."
),
"CUDA_ERROR_STREAM_CAPTURE_UNSUPPORTED".enum("This error indicates that the operation is not permitted when the stream is capturing.", "900"),
"CUDA_ERROR_STREAM_CAPTURE_INVALIDATED".enum(
"This error indicates that the current capture sequence on the stream has been invalidated due to a previous error."
),
"CUDA_ERROR_STREAM_CAPTURE_MERGE".enum("This error indicates that the operation would have resulted in a merge of two independent capture sequences."),
"CUDA_ERROR_STREAM_CAPTURE_UNMATCHED".enum("This error indicates that the capture was not initiated in this stream."),
"CUDA_ERROR_STREAM_CAPTURE_UNJOINED".enum("This error indicates that the capture sequence contains a fork that was not joined to the primary stream."),
"CUDA_ERROR_STREAM_CAPTURE_ISOLATION".enum(
"""
This error indicates that a dependency would have been created which crosses the capture sequence boundary. Only implicit in-stream ordering
dependencies are allowed to cross the boundary.
"""
),
"CUDA_ERROR_STREAM_CAPTURE_IMPLICIT".enum(
"This error indicates a disallowed implicit dependency on a current capture sequence from cudaStreamLegacy."
),
"CUDA_ERROR_CAPTURED_EVENT".enum(
"This error indicates that the operation is not permitted on an event which was last recorded in a capturing stream."
),
"CUDA_ERROR_STREAM_CAPTURE_WRONG_THREAD".enum(
"""
A stream capture sequence not initiated with the #STREAM_CAPTURE_MODE_RELAXED argument to #StreamBeginCapture() was passed to #StreamEndCapture()
in a different thread.
"""
),
"CUDA_ERROR_TIMEOUT".enum("This error indicates that the timeout specified for the wait operation has lapsed."),
"CUDA_ERROR_GRAPH_EXEC_UPDATE_FAILURE".enum(
"""
This error indicates that the graph update was not performed because it included changes which violated constraints specific to instantiated graph
update.
"""
),
"CUDA_ERROR_EXTERNAL_DEVICE".enum(
"""
This indicates that an async error has occurred in a device outside of CUDA. If CUDA was waiting for an external device's signal before consuming
shared data, the external device signaled an error indicating that the data is not valid for consumption. This leaves the process in an
inconsistent state and any further CUDA work will return the same error. To continue using CUDA, the process must be terminated and relaunched.
"""
),
"CUDA_ERROR_UNKNOWN".enum("This indicates that an unknown internal error has occurred.", "999")
).noPrefix()
EnumConstant(
"P2P Attributes. ({@code CUdevice_P2PAttribute})",
"DEVICE_P2P_ATTRIBUTE_PERFORMANCE_RANK".enum("A relative value indicating the performance of the link between two devices", 0x01),
"DEVICE_P2P_ATTRIBUTE_ACCESS_SUPPORTED".enum("P2P Access is enable", 0x02),
"DEVICE_P2P_ATTRIBUTE_NATIVE_ATOMIC_SUPPORTED".enum("Atomic operation over the link supported", 0x03),
"DEVICE_P2P_ATTRIBUTE_ACCESS_ACCESS_SUPPORTED".enum("Deprecated, use CU_DEVICE_P2P_ATTRIBUTE_CUDA_ARRAY_ACCESS_SUPPORTED instead", 0x04),
"DEVICE_P2P_ATTRIBUTE_CUDA_ARRAY_ACCESS_SUPPORTED".enum("Accessing CUDA arrays over the link supported", 0x04)
)
EnumConstant(
"Flags for #MemHostAlloc().",
"MEMHOSTALLOC_PORTABLE".enum("If set, host memory is portable between CUDA contexts.", 0x01),
"MEMHOSTALLOC_DEVICEMAP".enum(
"If set, host memory is mapped into CUDA address space and #MemHostGetDevicePointer() may be called on the host pointer.",
0x02
),
"MEMHOSTALLOC_WRITECOMBINED".enum(
"""
If set, host memory is allocated as write-combined - fast to write, faster to DMA, slow to read except via SSE4 streaming load instruction
({@code MOVNTDQA}).
""",
0x04
)
)
EnumConstant(
"Flags for #MemHostRegister().",
"MEMHOSTREGISTER_PORTABLE".enum("If set, host memory is portable between CUDA contexts.", "0x01"),
"MEMHOSTREGISTER_DEVICEMAP".enum(
"If set, host memory is mapped into CUDA address space and #MemHostGetDevicePointer() may be called on the host pointer.",
"0x02"
),
"MEMHOSTREGISTER_IOMEMORY".enum(
"""
If set, the passed memory pointer is treated as pointing to some memory-mapped I/O space, e.g. belonging to a third-party PCIe device.
On Windows the flag is a no-op. On Linux that memory is marked as non cache-coherent for the GPU and is expected to be physically contiguous.
It may return #CUDA_ERROR_NOT_PERMITTED if run as an unprivileged user, #CUDA_ERROR_NOT_SUPPORTED on older Linux kernel versions. On all other
platforms, it is not supported and #CUDA_ERROR_NOT_SUPPORTED is returned.
""",
"0x04"
),
"MEMHOSTREGISTER_READ_ONLY".enum(
"""
If set, the passed memory pointer is treated as pointing to memory that is considered read-only by the device.
On platforms without #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLES, this flag is required in order to register memory mapped to
the CPU as read-only. Support for the use of this flag can be queried from the device attribute
#DEVICE_ATTRIBUTE_READ_ONLY_HOST_REGISTER_SUPPORTED. Using this flag with a current context associated with a device that does not have this
attribute set will cause #MemHostRegister() to error with #CUDA_ERROR_NOT_SUPPORTED.
""",
"0x08"
)
)
IntConstant(
"Indicates that the layered sparse CUDA array or CUDA mipmapped array has a single mip tail region for all layers.",
"ARRAY_SPARSE_PROPERTIES_SINGLE_MIPTAIL".."0x1"
)
EnumConstant(
"Resource view format. ({@code CUresourceViewFormat})",
"RES_VIEW_FORMAT_NONE".enum("No resource view format (use underlying resource format)", 0x00),
"RES_VIEW_FORMAT_UINT_1X8".enum("1 channel unsigned 8-bit integers", 0x01),
"RES_VIEW_FORMAT_UINT_2X8".enum("2 channel unsigned 8-bit integers", 0x02),
"RES_VIEW_FORMAT_UINT_4X8".enum("4 channel unsigned 8-bit integers", 0x03),
"RES_VIEW_FORMAT_SINT_1X8".enum("1 channel signed 8-bit integers", 0x04),
"RES_VIEW_FORMAT_SINT_2X8".enum("2 channel signed 8-bit integers", 0x05),
"RES_VIEW_FORMAT_SINT_4X8".enum("4 channel signed 8-bit integers", 0x06),
"RES_VIEW_FORMAT_UINT_1X16".enum("1 channel unsigned 16-bit integers", 0x07),
"RES_VIEW_FORMAT_UINT_2X16".enum("2 channel unsigned 16-bit integers", 0x08),
"RES_VIEW_FORMAT_UINT_4X16".enum("4 channel unsigned 16-bit integers", 0x09),
"RES_VIEW_FORMAT_SINT_1X16".enum("1 channel signed 16-bit integers", 0x0a),
"RES_VIEW_FORMAT_SINT_2X16".enum("2 channel signed 16-bit integers", 0x0b),
"RES_VIEW_FORMAT_SINT_4X16".enum("4 channel signed 16-bit integers", 0x0c),
"RES_VIEW_FORMAT_UINT_1X32".enum("1 channel unsigned 32-bit integers", 0x0d),
"RES_VIEW_FORMAT_UINT_2X32".enum("2 channel unsigned 32-bit integers", 0x0e),
"RES_VIEW_FORMAT_UINT_4X32".enum("4 channel unsigned 32-bit integers", 0x0f),
"RES_VIEW_FORMAT_SINT_1X32".enum("1 channel signed 32-bit integers", 0x10),
"RES_VIEW_FORMAT_SINT_2X32".enum("2 channel signed 32-bit integers", 0x11),
"RES_VIEW_FORMAT_SINT_4X32".enum("4 channel signed 32-bit integers", 0x12),
"RES_VIEW_FORMAT_FLOAT_1X16".enum("1 channel 16-bit floating point", 0x13),
"RES_VIEW_FORMAT_FLOAT_2X16".enum("2 channel 16-bit floating point", 0x14),
"RES_VIEW_FORMAT_FLOAT_4X16".enum("4 channel 16-bit floating point", 0x15),
"RES_VIEW_FORMAT_FLOAT_1X32".enum("1 channel 32-bit floating point", 0x16),
"RES_VIEW_FORMAT_FLOAT_2X32".enum("2 channel 32-bit floating point", 0x17),
"RES_VIEW_FORMAT_FLOAT_4X32".enum("4 channel 32-bit floating point", 0x18),
"RES_VIEW_FORMAT_UNSIGNED_BC1".enum("Block compressed 1", 0x19),
"RES_VIEW_FORMAT_UNSIGNED_BC2".enum("Block compressed 2", 0x1a),
"RES_VIEW_FORMAT_UNSIGNED_BC3".enum("Block compressed 3", 0x1b),
"RES_VIEW_FORMAT_UNSIGNED_BC4".enum("Block compressed 4 unsigned", 0x1c),
"RES_VIEW_FORMAT_SIGNED_BC4".enum("Block compressed 4 signed", 0x1d),
"RES_VIEW_FORMAT_UNSIGNED_BC5".enum("Block compressed 5 unsigned", 0x1e),
"RES_VIEW_FORMAT_SIGNED_BC5".enum("Block compressed 5 signed", 0x1f),
"RES_VIEW_FORMAT_UNSIGNED_BC6H".enum("Block compressed 6 unsigned half-float", 0x20),
"RES_VIEW_FORMAT_SIGNED_BC6H".enum("Block compressed 6 signed half-float", 0x21),
"RES_VIEW_FORMAT_UNSIGNED_BC7".enum("Block compressed 7", 0x22)
)
EnumConstant(
"Access flags that specify the level of access the current context's device has on the memory referenced. ({@code CUDA_POINTER_ATTRIBUTE_ACCESS_FLAGS})",
"POINTER_ATTRIBUTE_ACCESS_FLAG_NONE".enum(
"""
No access, meaning the device cannot access this memory at all, thus must be staged through accessible memory in order to complete certain
operations
""",
"0x0"
),
"POINTER_ATTRIBUTE_ACCESS_FLAG_READ".enum(
"Read-only access, meaning writes to this memory are considered invalid accesses and thus return error in that case.",
"0x1"
),
"POINTER_ATTRIBUTE_ACCESS_FLAG_READWRITE".enum("Read-write access, the device has full read-write access to the memory", "0x3")
)
EnumConstant(
"External memory handle types. ({@code CUexternalMemoryHandleType})",
"EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD".enum("Handle is an opaque file descriptor", "1"),
"EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32".enum("Handle is an opaque shared NT handle"),
"EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT".enum("Handle is an opaque, globally shared handle"),
"EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_HEAP".enum("Handle is a D3D12 heap object"),
"EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE".enum("Handle is a D3D12 committed resource"),
"EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE".enum("Handle is a shared NT handle to a D3D11 resource"),
"EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE_KMT".enum("Handle is a globally shared handle to a D3D11 resource"),
"EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF".enum("Handle is an NvSciBuf object")
)
IntConstant(
"Indicates that the external memory object is a dedicated resource.",
"CUDA_EXTERNAL_MEMORY_DEDICATED"..0x1
).noPrefix()
IntConstant(
"""
When the {@code flags} parameter of ##CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS contains this flag, it indicates that signaling an external semaphore
object should skip performing appropriate memory synchronization operations over all the external memory objects that are imported as
#EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF, which otherwise are performed by default to ensure data coherency with other importers of the same
{@code NvSciBuf} memory objects.
""",
"CUDA_EXTERNAL_SEMAPHORE_SIGNAL_SKIP_NVSCIBUF_MEMSYNC".."0x01"
).noPrefix()
IntConstant(
"""
When the {@code flags} parameter of ##CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS contains this flag, it indicates that waiting on an external
semaphore object should skip performing appropriate memory synchronization operations over all the external memory objects that are imported as
#EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF, which otherwise are performed by default to ensure data coherency with other importers of the same
{@code NvSciBuf} memory objects.
""",
"CUDA_EXTERNAL_SEMAPHORE_WAIT_SKIP_NVSCIBUF_MEMSYNC".."0x02"
).noPrefix()
IntConstant(
"""
When {@code flags} of #DeviceGetNvSciSyncAttributes() is set to this, it indicates that application needs signaler specific
{@code NvSciSyncAttr} to be filled by {@code cuDeviceGetNvSciSyncAttributes}.
""",
"CUDA_NVSCISYNC_ATTR_SIGNAL".."0x1"
).noPrefix()
IntConstant(
"""
When {@code flags} of #DeviceGetNvSciSyncAttributes() is set to this, it indicates that application needs waiter specific {@code NvSciSyncAttr} to be
filled by {@code cuDeviceGetNvSciSyncAttributes}.
""",
"CUDA_NVSCISYNC_ATTR_WAIT".."0x2"
).noPrefix()
EnumConstant(
"External semaphore handle types. ({@code CUexternalSemaphoreHandleType})",
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD".enum("Handle is an opaque file descriptor", "1"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32".enum("Handle is an opaque shared NT handle"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT".enum("Handle is an opaque, globally shared handle"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE".enum("Handle is a shared NT handle referencing a D3D12 fence object"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_FENCE".enum("Handle is a shared NT handle referencing a D3D11 fence object"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC".enum("Opaque handle to NvSciSync Object"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX".enum("Handle is a shared NT handle referencing a D3D11 keyed mutex object"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX_KMT".enum("Handle is a globally shared handle referencing a D3D11 keyed mutex object"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD".enum("Handle is an opaque file descriptor referencing a timeline semaphore"),
"EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_WIN32".enum("Handle is an opaque shared NT handle referencing a timeline semaphore")
)
EnumConstant(
"Flags for specifying particular handle types. ({@code CUmemAllocationHandleType})",
"MEM_HANDLE_TYPE_NONE".enum("Does not allow any export mechanism.", "0x0"),
"MEM_HANDLE_TYPE_POSIX_FILE_DESCRIPTOR".enum("Allows a file descriptor to be used for exporting. Permitted only on POSIX systems. ({@code int})", "0x1"),
"MEM_HANDLE_TYPE_WIN32".enum("Allows a Win32 NT handle to be used for exporting. ({@code HANDLE})", "0x2"),
"MEM_HANDLE_TYPE_WIN32_KMT".enum("Allows a Win32 KMT handle to be used for exporting. ({@code D3DKMT_HANDLE})", "0x4")
)
EnumConstant(
"Specifies the memory protection flags for mapping. ({@code CUmemAccess_flags})",
"MEM_ACCESS_FLAGS_PROT_NONE".enum("Default, make the address range not accessible", "0x0"),
"MEM_ACCESS_FLAGS_PROT_READ".enum("Make the address range read accessible", "0x1"),
"MEM_ACCESS_FLAGS_PROT_READWRITE".enum("Make the address range read-write accessible", "0x3")
)
EnumConstant(
"Specifies the type of location. ({@code CUmemLocationType})",
"MEM_LOCATION_TYPE_INVALID".enum("", "0x0"),
"MEM_LOCATION_TYPE_DEVICE".enum("Location is a device location, thus id is a device ordinal", "0x1")
)
EnumConstant(
"Defines the allocation types available. ({@code CUmemAllocationType})",
"MEM_ALLOCATION_TYPE_INVALID".enum("", "0x0"),
"MEM_ALLOCATION_TYPE_PINNED".enum(
"This allocation type is 'pinned', i.e. cannot migrate from its current location while the application is actively using it",
"0x1"
)
)
EnumConstant(
"Flag for requesting different optimal and required granularities for an allocation. ({@code CUmemAllocationGranularity_flags})",
"MEM_ALLOC_GRANULARITY_MINIMUM".enum("Minimum required granularity for allocation", "0x0"),
"MEM_ALLOC_GRANULARITY_RECOMMENDED".enum("Recommended granularity for allocation for best performance", "0x1")
)
EnumConstant(
"Sparse subresource types. ({@code CUarraySparseSubresourceType})",
"ARRAY_SPARSE_SUBRESOURCE_TYPE_SPARSE_LEVEL".enum("", "0"),
"ARRAY_SPARSE_SUBRESOURCE_TYPE_MIPTAIL".enum
)
EnumConstant(
"Memory operation types. ({@code CUmemOperationType})",
"MEM_OPERATION_TYPE_MAP".enum("", "1"),
"MEM_OPERATION_TYPE_UNMAP".enum
)
EnumConstant(
"Memory handle types ({@code CUmemHandleType})",
"MEM_HANDLE_TYPE_GENERIC".enum("", "0")
)
EnumConstant(
"Specifies compression attribute for an allocation. ({@code CUmemAllocationCompType})",
"MEM_ALLOCATION_COMP_NONE".enum("Allocating non-compressible memory", "0x0"),
"MEM_ALLOCATION_COMP_GENERIC".enum("Allocating compressible memory", "0x1")
)
IntConstant(
"This flag if set indicates that the memory will be used as a tile pool.",
"MEM_CREATE_USAGE_TILE_POOL".."0x1"
)
EnumConstant(
"{@code CUgraphExecUpdateResult}",
"GRAPH_EXEC_UPDATE_SUCCESS".enum("The update succeeded", "0x0"),
"GRAPH_EXEC_UPDATE_ERROR".enum("The update failed for an unexpected reason which is described in the return value of the function", "0x1"),
"GRAPH_EXEC_UPDATE_ERROR_TOPOLOGY_CHANGED".enum("The update failed because the topology changed", "0x2"),
"GRAPH_EXEC_UPDATE_ERROR_NODE_TYPE_CHANGED".enum("The update failed because a node type changed", "0x3"),
"GRAPH_EXEC_UPDATE_ERROR_FUNCTION_CHANGED".enum("The update failed because the function of a kernel node changed (CUDA driver <11.2)", "0x4"),
"GRAPH_EXEC_UPDATE_ERROR_PARAMETERS_CHANGED".enum("The update failed because the parameters changed in a way that is not supported", "0x5"),
"GRAPH_EXEC_UPDATE_ERROR_NOT_SUPPORTED".enum("The update failed because something about the node is not supported", "0x6"),
"GRAPH_EXEC_UPDATE_ERROR_UNSUPPORTED_FUNCTION_CHANGE".enum(
"The update failed because the function of a kernel node changed in an unsupported way",
"0x7"
)
)
EnumConstant(
"CUDA memory pool attributes ({@code CUmemPool_attribute})",
"MEMPOOL_ATTR_REUSE_FOLLOW_EVENT_DEPENDENCIES".enum(
"""
Allow #MemAllocAsync() to use memory asynchronously freed in another streams as long as a stream ordering dependency of the allocating stream on
the free action exists. Cuda events and null stream interactions can create the required stream ordered dependencies.
(value type = {@code int}, default enabled)
""",
"1"
),
"MEMPOOL_ATTR_REUSE_ALLOW_OPPORTUNISTIC".enum(
"Allow reuse of already completed frees when there is no dependency between the free and allocation. (value type = {@code int}, default enabled)"
),
"MEMPOOL_ATTR_REUSE_ALLOW_INTERNAL_DEPENDENCIES".enum(
"""
Allow #MemAllocAsync() to insert new stream dependencies in order to establish the stream ordering required to reuse a piece of memory released by
#MemFreeAsync().
(value type = {@code int}, default enabled).
"""
),
"MEMPOOL_ATTR_RELEASE_THRESHOLD".enum(
"""
Amount of reserved memory in bytes to hold onto before trying to release memory back to the OS.
When more than the release threshold bytes of memory are held by the memory pool, the allocator will try to release memory back to the OS on the
next call to stream, event or context synchronize.
(value type = {@code cuuint64_t}, default 0)
"""
),
"MEMPOOL_ATTR_RESERVED_MEM_CURRENT".enum("Amount of backing memory currently allocated for the mempool. (value type = {@code cuuint64_t})"),
"MEMPOOL_ATTR_RESERVED_MEM_HIGH".enum(
"""
High watermark of backing memory allocated for the {@code mempool} since the last time it was reset. High watermark can only be reset to zero.
(value type = {@code cuuint64_t})
"""
),
"MEMPOOL_ATTR_USED_MEM_CURRENT".enum("Amount of memory from the pool that is currently in use by the application (value type = {@code cuuint64_t})."),
"MEMPOOL_ATTR_USED_MEM_HIGH".enum(
"""
High watermark of the amount of memory from the pool that was in use by the application since the last time it was reset. High watermark can only
be reset to zero.
(value type = {@code cuuint64_t})
"""
)
)
EnumConstant(
"{@code CUgraphMem_attribute}",
"GRAPH_MEM_ATTR_USED_MEM_CURRENT".enum("(value type = cuuint64_t) Amount of memory, in bytes, currently associated with graphs", "0"),
"GRAPH_MEM_ATTR_USED_MEM_HIGH".enum(
"""
High watermark of memory, in bytes, associated with graphs since the last time it was reset. High watermark can only be reset to zero.
(value type = {@code cuuint64_t})
"""
),
"GRAPH_MEM_ATTR_RESERVED_MEM_CURRENT".enum(
"Amount of memory, in bytes, currently allocated for use by the CUDA graphs asynchronous allocator. (value type = {@code cuuint64_t})"
),
"GRAPH_MEM_ATTR_RESERVED_MEM_HIGH".enum(
"High watermark of memory, in bytes, currently allocated for use by the CUDA graphs asynchronous allocator. (value type = {@code cuuint64_t})"
)
)
EnumConstant(
"",
"CUDA_COOPERATIVE_LAUNCH_MULTI_DEVICE_NO_PRE_LAUNCH_SYNC".enum(
"""
If set, each kernel launched as part of #LaunchCooperativeKernelMultiDevice() only waits for prior work in the stream corresponding to that GPU to
complete before the kernel begins execution.
""",
0x01
),
"CUDA_COOPERATIVE_LAUNCH_MULTI_DEVICE_NO_POST_LAUNCH_SYNC".enum(
"""
If set, any subsequent work pushed in a stream that participated in a call to #LaunchCooperativeKernelMultiDevice() will only wait for the kernel
launched on the GPU corresponding to that stream to complete before it begins execution.
""",
0x02
)
)
EnumConstant(
"",
"CUDA_ARRAY3D_LAYERED".enum(
"""
If set, the CUDA array is a collection of layers, where each layer is either a 1D or a 2D array and the Depth member of ##CUDA_ARRAY3D_DESCRIPTOR
specifies the number of layers, not the depth of a 3D array.
""",
"0x01"
),
"CUDA_ARRAY3D_2DARRAY".enum("Deprecated, use #CUDA_ARRAY3D_LAYERED.", "0x01"),
"CUDA_ARRAY3D_SURFACE_LDST".enum("This flag must be set in order to bind a surface reference to the CUDA array.", "0x02"),
"CUDA_ARRAY3D_CUBEMAP".enum(
"""
If set, the CUDA array is a collection of six 2D arrays, representing faces of a cube. The width of such a CUDA array must be equal to its height,
and Depth must be six. If #CUDA_ARRAY3D_LAYERED flag is also set, then the CUDA array is a collection of cubemaps and Depth must be a multiple of
six.
""",
"0x04"
),
"CUDA_ARRAY3D_TEXTURE_GATHER".enum("This flag must be set in order to perform texture gather operations on a CUDA array.", "0x08"),
"CUDA_ARRAY3D_DEPTH_TEXTURE".enum("This flag if set indicates that the CUDA array is a DEPTH_TEXTURE.", "0x10"),
"CUDA_ARRAY3D_COLOR_ATTACHMENT".enum("This flag indicates that the CUDA array may be bound as a color target in an external graphics API.", "0x20"),
"CUDA_ARRAY3D_SPARSE".enum(
"This flag if set indicates that the CUDA array or CUDA mipmapped array is a sparse CUDA array or CUDA mipmapped array respectively",
"0x40"
)
).noPrefix()
EnumConstant(
"Flag for #TexRefSetArray().",
"TRSA_OVERRIDE_FORMAT".enum("Override the {@code texref} format with a format inferred from the array.", 0x01)
)
EnumConstant(
"Flag for #TexRefSetFlags().",
"TRSF_READ_AS_INTEGER".enum("Read the texture as integers rather than promoting the values to floats in the range {@code [0,1]}.", "0x01"),
"TRSF_NORMALIZED_COORDINATES".enum("Use normalized texture coordinates in the range {@code [0,1)} instead of {@code [0,dim)}.", "0x02"),
"TRSF_SRGB".enum("Perform {@code sRGB->linear} conversion during texture read.", "0x10"),
"TRSF_DISABLE_TRILINEAR_OPTIMIZATION".enum("Disable any trilinear filtering optimizations.", "0x20")
)
LongConstant(
"End of array terminator for the {@code extra} parameter to #LaunchKernel().",
"LAUNCH_PARAM_END"..0x00L
)
LongConstant(
"""
Indicator that the next value in the {@code extra} parameter to #LaunchKernel() will be a pointer to a buffer containing all kernel parameters used for
launching kernel {@code f}.
This buffer needs to honor all alignment/padding requirements of the individual parameters. If #LAUNCH_PARAM_BUFFER_SIZE is not also specified in the
{@code extra} array, then #LAUNCH_PARAM_BUFFER_POINTER will have no effect.
""",
"LAUNCH_PARAM_BUFFER_POINTER"..0x01L
)
LongConstant(
"""
Indicator that the next value in the {@code extra} parameter to #LaunchKernel() will be a pointer to a {@code size_t} which contains the size of the
buffer specified with #LAUNCH_PARAM_BUFFER_POINTER.
It is required that {@code CU_LAUNCH_PARAM_BUFFER_POINTER} also be specified in the {@code extra} array if the value associated with
{@code CU_LAUNCH_PARAM_BUFFER_SIZE} is not zero.
""",
"LAUNCH_PARAM_BUFFER_SIZE"..0x02L
)
IntConstant(
"For texture references loaded into the module, use default texunit from texture reference.",
"PARAM_TR_DEFAULT".."-1"
)
IntConstant("Device that represents the CPU.", "DEVICE_CPU".."-1")
IntConstant("Device that represents an invalid device.", "DEVICE_INVALID".."-2")
EnumConstant(
"Bitmasks for #DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_FLUSH_WRITES_OPTIONS. ({@code CUflushGPUDirectRDMAWritesOptions})",
"FLUSH_GPU_DIRECT_RDMA_WRITES_OPTION_HOST".enum(
"#FlushGPUDirectRDMAWrites() and its CUDA Runtime API counterpart are supported on the device.",
"1<<0"
),
"FLUSH_GPU_DIRECT_RDMA_WRITES_OPTION_MEMOPS".enum(
"The #STREAM_WAIT_VALUE_FLUSH flag and the #STREAM_MEM_OP_FLUSH_REMOTE_WRITES {@code MemOp} are supported on the device.",
"1<<1"
)
)
EnumConstant(
"Platform native ordering for GPUDirect RDMA writes. ({@code CUGPUDirectRDMAWritesOrdering})",
"GPU_DIRECT_RDMA_WRITES_ORDERING_NONE".enum(
"The device does not natively support ordering of remote writes. #FlushGPUDirectRDMAWrites() can be leveraged if supported.",
"0"
),
"GPU_DIRECT_RDMA_WRITES_ORDERING_OWNER".enum(
"Natively, the device can consistently consume remote writes, although other CUDA devices may not.",
"100"
),
"GPU_DIRECT_RDMA_WRITES_ORDERING_ALL_DEVICES".enum("Any CUDA device in the system can consistently consume remote writes to this device.", "200")
)
EnumConstant(
"The scopes for #FlushGPUDirectRDMAWrites() ({@code CUflushGPUDirectRDMAWritesScope})",
"FLUSH_GPU_DIRECT_RDMA_WRITES_TO_OWNER".enum("Blocks until remote writes are visible to the CUDA device context owning the data.", "100"),
"FLUSH_GPU_DIRECT_RDMA_WRITES_TO_ALL_DEVICES".enum("Blocks until remote writes are visible to all CUDA device contexts.", "200")
)
EnumConstant(
"The targets for #FlushGPUDirectRDMAWrites() ({@code CUflushGPUDirectRDMAWritesTarget})",
"FLUSH_GPU_DIRECT_RDMA_WRITES_TARGET_CURRENT_CTX".enum(
"Sets the target for {@code cuFlushGPUDirectRDMAWrites()} to the currently active CUDA device context.",
"0"
)
)
EnumConstant(
"The additional write options for #GraphDebugDotPrint() ({@code CUgraphDebugDot_flags})",
"GRAPH_DEBUG_DOT_FLAGS_VERBOSE".enum("", "1<<0"),
"GRAPH_DEBUG_DOT_FLAGS_RUNTIME_TYPES".enum("Output all debug data as if every debug flag is enabled", "1<<1"),
"GRAPH_DEBUG_DOT_FLAGS_KERNEL_NODE_PARAMS".enum("Use CUDA Runtime structures for output", "1<<2"),
"GRAPH_DEBUG_DOT_FLAGS_MEMCPY_NODE_PARAMS".enum("Adds ##CUDA_KERNEL_NODE_PARAMS values to output", "1<<3"),
"GRAPH_DEBUG_DOT_FLAGS_MEMSET_NODE_PARAMS".enum("Adds ##CUDA_MEMCPY3D values to output", "1<<4"),
"GRAPH_DEBUG_DOT_FLAGS_HOST_NODE_PARAMS".enum("Adds ##CUDA_MEMSET_NODE_PARAMS values to output", "1<<5"),
"GRAPH_DEBUG_DOT_FLAGS_EVENT_NODE_PARAMS".enum("Adds ##CUDA_HOST_NODE_PARAMS values to output", "1<<6"),
"GRAPH_DEBUG_DOT_FLAGS_EXT_SEMAS_SIGNAL_NODE_PARAMS".enum("Adds {@code CUevent} handle from record and wait nodes to output", "1<<7"),
"GRAPH_DEBUG_DOT_FLAGS_EXT_SEMAS_WAIT_NODE_PARAMS".enum("Adds ##CUDA_EXT_SEM_SIGNAL_NODE_PARAMS values to output", "1<<8"),
"GRAPH_DEBUG_DOT_FLAGS_KERNEL_NODE_ATTRIBUTES".enum("Adds ##CUDA_EXT_SEM_WAIT_NODE_PARAMS values to output", "1<<9"),
"GRAPH_DEBUG_DOT_FLAGS_HANDLES".enum("Adds {@code CUkernelNodeAttrValue} values to output", "1<<10"),
"GRAPH_DEBUG_DOT_FLAGS_MEM_ALLOC_NODE_PARAMS".enum("Adds node handles and every kernel function handle to output", "1<<11"),
"GRAPH_DEBUG_DOT_FLAGS_MEM_FREE_NODE_PARAMS".enum("Adds memory alloc node parameters to output", "1<<12")
)
EnumConstant(
"Flags for user objects for graphs. ({@code CUuserObject_flags})",
"USER_OBJECT_NO_DESTRUCTOR_SYNC".enum("Indicates the destructor execution is not synchronized by any CUDA handle.", "1")
)
EnumConstant(
"Flags for retaining user object references for graphs. ({@code CUuserObjectRetain_flags})",
"GRAPH_USER_OBJECT_MOVE".enum("Transfer references from the caller rather than creating new references.", "1")
)
EnumConstant(
"Flags for instantiating a graph. ({@code CUgraphInstantiate_flags})",
"CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH".enum("Automatically free memory allocated in a graph before relaunching.", "1")
).noPrefix()
CUresult(
"GetErrorString",
"""
Gets the string description of an error code.
Sets {@code *pStr} to the address of a NULL-terminated string description of the error code {@code error}. If the error code is not recognized,
#CUDA_ERROR_INVALID_VALUE will be returned and {@code *pStr} will be set to the #NULL address.
""",
CUresult("error", "error code to convert to string"),
Check(1)..charASCII.const.p.p("pStr", "address of the string pointer")
)
CUresult(
"GetErrorName",
"""
Gets the string representation of an error code enum name.
Sets {@code *pStr} to the address of a NULL-terminated string representation of the name of the enum error code {@code error}. If the error code is not
recognized, #CUDA_ERROR_INVALID_VALUE will be returned and {@code *pStr} will be set to the #NULL address.
""",
CUresult("error", "error code to convert to string"),
Check(1)..charASCII.const.p.p("pStr", "address of the string pointer")
)
CUresult(
"Init",
"""
Initialize the CUDA driver API.
Initializes the driver API and must be called before any other function from the driver API. Currently, the {@code Flags} parameter must be 0. If
{@code cuInit()} has not been called, any function from the driver API will return #CUDA_ERROR_NOT_INITIALIZED.
""",
unsigned_int("Flags", "initialization flag for CUDA")
)
CUresult(
"DriverGetVersion",
"""
Returns the latest CUDA version supported by driver.
Returns in {@code *driverVersion} the version of CUDA supported by the driver. The version is returned as ({@code 1000 × major + 10 × minor}). For
example, CUDA 9.2 would be represented by 9020.
This function automatically returns #CUDA_ERROR_INVALID_VALUE if {@code driverVersion} is #NULL.
""",
Check(1)..int.p("driverVersion", "returns the CUDA driver version")
)
CUresult(
"DeviceGet",
"""
Returns a handle to a compute device.
Returns in {@code *device} a device handle given an ordinal in the range {@code [0, cuDeviceGetCount()-1]}.
""",
Check(1)..CUdevice.p("device", "returned device handle"),
int("ordinal", "device number to get handle for")
)
CUresult(
"DeviceGetCount",
"""
Returns the number of compute-capable devices.
Returns in {@code *count} the number of devices with compute capability greater than or equal to 2.0 that are available for execution. If there is no
such device, {@code cuDeviceGetCount()} returns 0.
""",
Check(1)..int.p("count", "returned number of compute-capable devices")
)
CUresult(
"DeviceGetName",
"""
Returns an identifer string for the device.
Returns an ASCII string identifying the device {@code dev} in the NULL-terminated string pointed to by {@code name}. {@code len} specifies the maximum
length of the string that may be returned.
""",
charASCII.p("name", "returned identifier string for the device"),
AutoSize("name")..int("len", "maximum length of string to store in {@code name}"),
CUdevice("dev", "device to get identifier string for")
)
IgnoreMissing..CUresult(
"DeviceGetUuid",
"""
Return an UUID for the device.
Note there is a later version of this API, #DeviceGetUuid_v2(). It will supplant this version in 12.0, which is retained for minor version.
compatibility.
Returns 16-octets identifing the device {@code dev} in the structure pointed by the {@code uuid}.
""",
CUuuid.p("uuid", "returned UUID"),
CUdevice("dev", "device to get identifier string for")
)
IgnoreMissing..CUresult(
"DeviceGetUuid_v2",
"""
Return an UUID for the device (11.4+).
Returns 16-octets identifing the device {@code dev} in the structure pointed by the {@code uuid}. If the device is in MIG mode, returns its MIG UUID
which uniquely identifies the subscribed MIG compute instance.
""",
CUuuid.p("uuid", "returned UUID"),
CUdevice("dev", "device to get identifier string for")
)
IgnoreMissing..CUresult(
"DeviceGetLuid",
"""
Return an LUID and device node mask for the device
Return identifying information ({@code luid} and {@code deviceNodeMask}) to allow matching device with graphics APIs.
""",
Unsafe..char.p("luid", "returned LUID"),
Check(1)..unsigned_int.p("deviceNodeMask", "returned device node mask"),
CUdevice("dev", "device to get identifier string for")
)
CUresult(
"DeviceTotalMem",
"""
Returns the total amount of memory on the device
Returns in {@code *bytes} the total amount of memory available on the device {@code dev} in bytes.
""",
Check(1)..size_t.p("bytes", "returned memory available on device in bytes"),
CUdevice("dev", "device handle")
).versioned()
IgnoreMissing..CUresult(
"DeviceGetTexture1DLinearMaxWidth",
"""
Returns the maximum number of elements allocatable in a 1D linear texture for a given texture element size.
Returns in {@code maxWidthInElements} the maximum number of texture elements allocatable in a 1D linear texture for given {@code format} and {@code
numChannels}.
""",
Check(1)..size_t.p("maxWidthInElements", "returned maximum number of texture elements allocatable for given {@code format} and {@code numChannels}"),
CUarray_format("format", "texture format"),
unsigned_int("numChannels", "number of channels per texture element"),
CUdevice("dev", "device handle")
)
CUresult(
"DeviceGetAttribute",
"""
Returns information about the device.
Returns in {@code *pi} the integer value of the attribute {@code attrib} on device {@code dev}. The supported attributes are:
""",
Check(1)..int.p("pi", "returned device attribute value"),
CUdevice_attribute("attrib", "device attribute to query"),
CUdevice("dev", "device handle")
)
IgnoreMissing..CUresult(
"DeviceGetNvSciSyncAttributes",
"""
Return {@code NvSciSync} attributes that this device can support.
Returns in {@code nvSciSyncAttrList}, the properties of {@code NvSciSync} that this CUDA device, {@code dev} can support. The returned {@code
nvSciSyncAttrList} can be used to create an {@code NvSciSync} object that matches this device's capabilities.
If {@code NvSciSyncAttrKey_RequiredPerm} field in {@code nvSciSyncAttrList} is already set this API will return #CUDA_ERROR_INVALID_VALUE.
The applications should set {@code nvSciSyncAttrList} to a valid {@code NvSciSyncAttrList} failing which this API will return
#CUDA_ERROR_INVALID_HANDLE.
The {@code flags} controls how applications intends to use the {@code NvSciSync} created from the {@code nvSciSyncAttrList}. The valid flags are:
${ul(
"#CUDA_NVSCISYNC_ATTR_SIGNAL, specifies that the applications intends to signal an {@code NvSciSync} on this CUDA device.",
"#CUDA_NVSCISYNC_ATTR_WAIT, specifies that the applications intends to wait on an {@code NvSciSync} on this CUDA device."
)}
At least one of these flags must be set, failing which the API returns #CUDA_ERROR_INVALID_VALUE. Both the flags are orthogonal to one another: a
developer may set both these flags that allows to set both wait and signal specific attributes in the same {@code nvSciSyncAttrList}.
""",
Unsafe..void.p("nvSciSyncAttrList", "return NvSciSync attributes supported"),
CUdevice("dev", "valid Cuda Device to get {@code NvSciSync} attributes for"),
int("flags", "flags describing {@code NvSciSync} usage")
)
IgnoreMissing..CUresult(
"DeviceSetMemPool",
"""
Sets the current memory pool of a device
The memory pool must be local to the specified device. #MemAllocAsync() allocates from the current mempool of the provided stream's device. By default,
a device's current memory pool is its default memory pool.
${note("""Use #MemAllocFromPoolAsync() to specify asynchronous allocations from a device different than the one the stream runs on.""")}
""",
CUdevice("dev", ""),
CUmemoryPool("pool", "")
)
IgnoreMissing..CUresult(
"DeviceGetMemPool",
"""
Gets the current mempool for a device.
Returns the last pool provided to #DeviceSetMemPool() for this device or the device's default memory pool if #DeviceSetMemPool() has never been called.
By default the current mempool is the default mempool for a device. Otherwise the returned pool must have been set with #DeviceSetMemPool().
""",
Check(1)..CUmemoryPool.p("pool", ""),
CUdevice("dev", "")
)
IgnoreMissing..CUresult(
"DeviceGetDefaultMemPool",
"""
Returns the default mempool of a device.
The default mempool of a device contains device memory from that device.
""",
Check(1)..CUmemoryPool.p("pool_out", ""),
CUdevice("dev", "")
)
IgnoreMissing..CUresult(
"FlushGPUDirectRDMAWrites",
"""
Blocks until remote writes are visible to the specified scope.
Blocks until GPUDirect RDMA writes to the target context via mappings created through APIs like nvidia_p2p_get_pages (see
${url("https://docs.nvidia.com/cuda/gpudirect-rdma")} for more information), are visible to the specified scope.
If the scope equals or lies within the scope indicated by #DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_WRITES_ORDERING, the call will be a no-op and can be safely
omitted for performance. This can be determined by comparing the numerical values between the two enums, with smaller scopes having smaller values.
Users may query support for this API via #DEVICE_ATTRIBUTE_GPU_DIRECT_RDMA_FLUSH_WRITES_OPTIONS.
""",
CUflushGPUDirectRDMAWritesTarget("target", "the target of the operation, see {@code CUflushGPUDirectRDMAWritesTarget}"),
CUflushGPUDirectRDMAWritesScope("scope", "the scope of the operation, see {@code CUflushGPUDirectRDMAWritesScope}")
)
CUresult(
"DeviceGetProperties",
"""
Returns properties for a selected device.
Deprecated: This function was deprecated as of CUDA 5.0 and replaced by #DeviceGetAttribute().
Returns in {@code *prop} the properties of device {@code dev}.
""",
CUdevprop.p("prop", "returned properties of device"),
CUdevice("dev", "device to get properties for")
)
CUresult(
"DeviceComputeCapability",
"""
Returns the compute capability of the device.
Deprecated: This function was deprecated as of CUDA 5.0 and its functionality superceded by #DeviceGetAttribute().
Returns in {@code *major} and {@code *minor} the major and minor revision numbers that define the compute capability of the device {@code dev}.
""",
Check(1)..int.p("major", "major revision number"),
Check(1)..int.p("minor", "minor revision number"),
CUdevice("dev", "device handle")
)
IgnoreMissing..CUresult(
"DevicePrimaryCtxRetain",
"""
Retain the primary context on the GPU.
Retains the primary context on the device. Once the user successfully retains the primary context, the primary context will be active and available to
the user until the user releases it with #DevicePrimaryCtxRelease() or resets it with #DevicePrimaryCtxReset(). Unlike #CtxCreate() the newly retained
context is not pushed onto the stack.
Retaining the primary context for the first time will fail with #CUDA_ERROR_UNKNOWN if the compute mode of the device is #COMPUTEMODE_PROHIBITED. The
function #DeviceGetAttribute() can be used with #DEVICE_ATTRIBUTE_COMPUTE_MODE to determine the compute mode of the device. The <i>nvidia-smi</i> tool
can be used to set the compute mode for devices. Documentation for <i>nvidia-smi</i> can be obtained by passing a -h option to it.
Please note that the primary context always supports pinned allocations. Other flags can be specified by #DevicePrimaryCtxSetFlags().
""",
Check(1)..CUcontext.p("pctx", "returned context handle of the new context"),
CUdevice("dev", "device for which primary context is requested")
)
IgnoreMissing..CUresult(
"DevicePrimaryCtxRelease",
"""
Release the primary context on the GPU.
Releases the primary context interop on the device. A retained context should always be released once the user is done using it. The context is
automatically reset once the last reference to it is released. This behavior is different when the primary context was retained by the CUDA runtime
from CUDA 4.0 and earlier. In this case, the primary context remains always active.
Releasing a primary context that has not been previously retained will fail with #CUDA_ERROR_INVALID_CONTEXT.
Please note that unlike #CtxDestroy() this method does not pop the context from stack in any circumstances.
""",
CUdevice("dev", "device which primary context is released")
).versioned()
IgnoreMissing..CUresult(
"DevicePrimaryCtxSetFlags",
"""
Set flags for the primary context.
Sets the flags for the primary context on the device overwriting perviously set ones.
The three LSBs of the {@code flags} parameter can be used to control how the OS thread, which owns the CUDA context at the time of an API call,
interacts with the OS scheduler when waiting for results from the GPU. Only one of the scheduling flags can be set when creating a context:
${ul(
"""
#CTX_SCHED_SPIN: Instruct CUDA to actively spin when waiting for results from the GPU. This can decrease latency when waiting for the GPU, but
may lower the performance of CPU threads if they are performing work in parallel with the CUDA thread.
""",
"""
#CTX_SCHED_YIELD: Instruct CUDA to yield its thread when waiting for results from the GPU. This can increase latency when waiting for the GPU,
but can increase the performance of CPU threads performing work in parallel with the GPU.
""",
"#CTX_SCHED_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.",
"""
#CTX_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.
<b> Deprecated:</b> This flag was deprecated as of CUDA 4.0 and was replaced with #CTX_SCHED_BLOCKING_SYNC.
""",
"""
#CTX_SCHED_AUTO: The default value if the {@code flags} parameter is zero, uses a heuristic based on the number of active CUDA contexts in the
process <em>C</em> and the number of logical processors in the system <em>P</em>. If <em>C</em> > <em>P</em>, then CUDA will yield to other OS
threads when waiting for the GPU (#CTX_SCHED_YIELD), otherwise CUDA will not yield while waiting for results and actively spin on the processor
(#CTX_SCHED_SPIN). Additionally, on Tegra devices, #CTX_SCHED_AUTO uses a heuristic based on the power profile of the platform and may choose
#CTX_SCHED_BLOCKING_SYNC for low-powered devices.
""",
"""
#CTX_LMEM_RESIZE_TO_MAX: Instruct CUDA to not reduce local memory after resizing local memory for a kernel. This can prevent thrashing by local
memory allocations when launching many kernels with high local memory usage at the cost of potentially increased memory usage.
<b> Deprecated:</b> This flag is deprecated and the behavior enabled by this flag is now the default and cannot be disabled.
"""
)}
""",
CUdevice("dev", "device for which the primary context flags are set"),
unsigned_int("flags", "new flags for the device")
).versioned()
IgnoreMissing..CUresult(
"DevicePrimaryCtxGetState",
"""
Get the state of the primary context.
Returns in {@code *flags} the flags for the primary context of {@code dev}, and in {@code *active} whether it is active. See
#DevicePrimaryCtxSetFlags() for flag values.
""",
CUdevice("dev", "device to get primary context flags for"),
Check(1)..unsigned_int.p("flags", "pointer to store flags"),
Check(1)..int.p("active", "pointer to store context state; 0 = inactive, 1 = active")
)
IgnoreMissing..CUresult(
"DevicePrimaryCtxReset",
"""
Destroy all allocations and reset all state on the primary context.
Explicitly destroys and cleans up all resources associated with the current device in the current process.
Note that it is responsibility of the calling function to ensure that no other module in the process is using the device any more. For that reason it
is recommended to use #DevicePrimaryCtxRelease() in most cases. However it is safe for other modules to call {@code cuDevicePrimaryCtxRelease()} even
after resetting the device. Resetting the primary context does not release it, an application that has retained the primary context should explicitly
release its usage.
""",
CUdevice("dev", "device for which primary context is destroyed")
).versioned()
IgnoreMissing..CUresult(
"DeviceGetExecAffinitySupport",
"""
Returns information about the execution affinity support of the device.
Returns in {@code *pi} whether execution affinity type {@code type} is supported by device {@code dev}. The supported types are:
${ul(
"#EXEC_AFFINITY_TYPE_SM_COUNT: 1 if context with limited SMs is supported by the device, or 0 if not;"
)}
""",
Check(1)..int.p("pi", "1 if the execution affinity type {@code type} is supported by the device, or 0 if not"),
CUexecAffinityType("type", "execution affinity type to query"),
CUdevice("dev", "device handle")
)
CUresult(
"CtxCreate",
"""
Create a CUDA context.
${note("""In most cases it is recommended to use #DevicePrimaryCtxRetain().""")}
Creates a new CUDA context and associates it with the calling thread. The {@code flags} parameter is described below. The context is created with a
usage count of 1 and the caller of {@code cuCtxCreate()} must call #CtxDestroy() or when done using the context. If a context is already current to the
thread, it is supplanted by the newly created context and may be restored by a subsequent call to #CtxPopCurrent().
The three LSBs of the {@code flags} parameter can be used to control how the OS thread, which owns the CUDA context at the time of an API call,
interacts with the OS scheduler when waiting for results from the GPU. Only one of the scheduling flags can be set when creating a context:
${ul(
"""
#CTX_SCHED_SPIN: Instruct CUDA to actively spin when waiting for results from the GPU. This can decrease latency when waiting for the GPU, but may
lower the performance of CPU threads if they are performing work in parallel with the CUDA thread.
""",
"""
#CTX_SCHED_YIELD: Instruct CUDA to yield its thread when waiting for results from the GPU. This can increase latency when waiting for the GPU, but
can increase the performance of CPU threads performing work in parallel with the GPU.
""",
"#CTX_SCHED_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.",
"""
#CTX_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.
<b> Deprecated:</b> This flag was deprecated as of CUDA 4.0 and was replaced with #CTX_SCHED_BLOCKING_SYNC.
""",
"""
#CTX_SCHED_AUTO: The default value if the {@code flags} parameter is zero, uses a heuristic based on the number of active CUDA contexts in the
process <em>C</em> and the number of logical processors in the system <em>P</em>. If <em>C</em> > <em>P</em>, then CUDA will yield to other OS
threads when waiting for the GPU (#CTX_SCHED_YIELD), otherwise CUDA will not yield while waiting for results and actively spin on the processor
(#CTX_SCHED_SPIN). Additionally, on Tegra devices, #CTX_SCHED_AUTO uses a heuristic based on the power profile of the platform and may choose
#CTX_SCHED_BLOCKING_SYNC for low-powered devices.
""",
"""
#CTX_MAP_HOST: Instruct CUDA to support mapped pinned allocations. This flag must be set in order to allocate pinned host memory that is
accessible to the GPU.
""",
"""
#CTX_LMEM_RESIZE_TO_MAX: Instruct CUDA to not reduce local memory after resizing local memory for a kernel. This can prevent thrashing by local
memory allocations when launching many kernels with high local memory usage at the cost of potentially increased memory usage.
<b> Deprecated:</b> This flag is deprecated and the behavior enabled by this flag is now the default and cannot be disabled. Instead, the
per-thread stack size can be controlled with #CtxSetLimit().
"""
)}
Context creation will fail with #CUDA_ERROR_UNKNOWN if the compute mode of the device is #COMPUTEMODE_PROHIBITED. The function #DeviceGetAttribute()
can be used with #DEVICE_ATTRIBUTE_COMPUTE_MODE to determine the compute mode of the device. The <i>nvidia-smi</i> tool can be used to set the compute
mode for * devices. Documentation for <i>nvidia-smi</i> can be obtained by passing a -h option to it.
""",
Check(1)..CUcontext.p("pctx", "returned context handle of the new context"),
unsigned_int("flags", "context creation flags"),
CUdevice("dev", "device to create context on")
).versioned()
IgnoreMissing..CUresult(
"CtxCreate_v3",
"""
Create a CUDA context with execution affinity.
Creates a new CUDA context with execution affinity and associates it with the calling thread. The {@code paramsArray} and {@code flags} parameter are
described below. The context is created with a usage count of 1 and the caller of #CtxCreate() must call #CtxDestroy() or when done using the
context. If a context is already current to the thread, it is supplanted by the newly created context and may be restored by a subsequent call to
#CtxPopCurrent().
The type and the amount of execution resource the context can use is limited by {@code paramsArray} and {@code numParams}. The {@code paramsArray} is
an array of {@code CUexecAffinityParam} and the {@code numParams} describes the size of the array. If two {@code CUexecAffinityParam} in the array have
the same type, the latter execution affinity parameter overrides the former execution affinity parameter. The supported execution affinity types are:
${ul(
"""
#EXEC_AFFINITY_TYPE_SM_COUNT limits the portion of SMs that the context can use. The portion of SMs is specified as the number of SMs via
##CUexecAffinitySmCount. This limit will be internally rounded up to the next hardware-supported amount. Hence, it is imperative to query the
actual execution affinity of the context via #CtxGetExecAffinity()) after context creation. Currently, this attribute is only supported under
Volta+ MPS.
"""
)}
The three LSBs of the {@code flags} parameter can be used to control how the OS thread, which owns the CUDA context at the time of an API call,
interacts with the OS scheduler when waiting for results from the GPU. Only one of the scheduling flags can be set when creating a context:
${ul(
"""
#CTX_SCHED_SPIN: Instruct CUDA to actively spin when waiting for results from the GPU. This can decrease latency when waiting for the GPU, but may
lower the performance of CPU threads if they are performing work in parallel with the CUDA thread.
""",
"""
#CTX_SCHED_YIELD: Instruct CUDA to yield its thread when waiting for results from the GPU. This can increase latency when waiting for the GPU, but
can increase the performance of CPU threads performing work in parallel with the GPU.
""",
"#CTX_SCHED_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.",
"""
#CTX_BLOCKING_SYNC: Instruct CUDA to block the CPU thread on a synchronization primitive when waiting for the GPU to finish work.
<b> Deprecated:</b> This flag was deprecated as of CUDA 4.0 and was replaced with #CTX_SCHED_BLOCKING_SYNC.
""",
"""
#CTX_SCHED_AUTO: The default value if the {@code flags} parameter is zero, uses a heuristic based on the number of active CUDA contexts in the
process <em>C</em> and the number of logical processors in the system <em>P</em>. If <em>C</em> > <em>P</em>, then CUDA will yield to other OS
threads when waiting for the GPU (#CTX_SCHED_YIELD), otherwise CUDA will not yield while waiting for results and actively spin on the processor
(#CTX_SCHED_SPIN). Additionally, on Tegra devices, #CTX_SCHED_AUTO uses a heuristic based on the power profile of the platform and may choose
#CTX_SCHED_BLOCKING_SYNC for low-powered devices.
""",
"""
#CTX_MAP_HOST: Instruct CUDA to support mapped pinned allocations. This flag must be set in order to allocate pinned host memory that is accessible
to the GPU.
""",
"""
#CTX_LMEM_RESIZE_TO_MAX: Instruct CUDA to not reduce local memory after resizing local memory for a kernel. This can prevent thrashing by local
memory allocations when launching many kernels with high local memory usage at the cost of potentially increased memory usage.
<b> Deprecated:</b> This flag is deprecated and the behavior enabled by this flag is now the default and cannot be disabled. Instead, the
per-thread stack size can be controlled with #CtxSetLimit().
"""
)}
Context creation will fail with #CUDA_ERROR_UNKNOWN if the compute mode of the device is #COMPUTEMODE_PROHIBITED. The function #DeviceGetAttribute()
can be used with #DEVICE_ATTRIBUTE_COMPUTE_MODE to determine the compute mode of the device. The <i>nvidia-smi</i> tool can be used to set the compute
mode for * devices. Documentation for <i>nvidia-smi</i> can be obtained by passing a -h option to it.
""",
Check(1)..CUcontext.p("pctx", "returned context handle of the new context"),
CUexecAffinityParam.p("paramsArray", "execution affinity parameters"),
AutoSize("paramsArray")..int("numParams", "number of execution affinity parameters"),
unsigned_int("flags", "context creation flags"),
CUdevice("dev", "device to create context on")
)
IgnoreMissing..CUresult(
"CtxDestroy",
"""
Destroy a CUDA context.
Destroys the CUDA context specified by {@code ctx}. The context {@code ctx} will be destroyed regardless of how many threads it is current to. It is
the responsibility of the calling function to ensure that no API call issues using {@code ctx} while {@code cuCtxDestroy()} is executing.
Destroys and cleans up all resources associated with the context. It is the caller's responsibility to ensure that the context or its resources are not
accessed or passed in subsequent API calls and doing so will result in undefined behavior. These resources include CUDA types such as {@code CUmodule},
{@code CUfunction}, {@code CUstream}, {@code CUevent}, {@code CUarray}, {@code CUmipmappedArray}, {@code CUtexObject}, {@code CUsurfObject},
{@code CUtexref}, {@code CUsurfref}, {@code CUgraphicsResource}, {@code CUlinkState}, {@code CUexternalMemory} and {@code CUexternalSemaphore}.
If {@code ctx} is current to the calling thread then {@code ctx} will also be popped from the current thread's context stack (as though
#CtxPopCurrent() were called). If {@code ctx} is current to other threads, then {@code ctx} will remain current to those threads, and attempting to
access {@code ctx} from those threads will result in the error #CUDA_ERROR_CONTEXT_IS_DESTROYED.
""",
CUcontext("ctx", "context to destroy")
).versioned()
IgnoreMissing..CUresult(
"CtxPushCurrent",
"""
Pushes a context on the current CPU thread.
Pushes the given context {@code ctx} onto the CPU thread's stack of current contexts. The specified context becomes the CPU thread's current context,
so all CUDA functions that operate on the current context are affected.
The previous current context may be made current again by calling #CtxDestroy() or #CtxPopCurrent().
""",
CUcontext("ctx", "context to push")
).versioned()
IgnoreMissing..CUresult(
"CtxPopCurrent",
"""
Pops the current CUDA context from the current CPU thread.
Pops the current CUDA context from the CPU thread and passes back the old context handle in {@code *pctx}. That context may then be made current to a
different CPU thread by calling #CtxPushCurrent().
If a context was current to the CPU thread before #CtxCreate() or #CtxPushCurrent() was called, this function makes that context current to the CPU
thread again.
""",
Check(1)..CUcontext.p("pctx", "returned new context handle")
).versioned()
IgnoreMissing..CUresult(
"CtxSetCurrent",
"""
Binds the specified CUDA context to the calling CPU thread.
Binds the specified CUDA context to the calling CPU thread. If {@code ctx} is #NULL then the CUDA context previously bound to the calling CPU thread is
unbound and #CUDA_SUCCESS is returned.
If there exists a CUDA context stack on the calling CPU thread, this will replace the top of that stack with {@code ctx}. If {@code ctx} is #NULL then
this will be equivalent to popping the top of the calling CPU thread's CUDA context stack (or a no-op if the calling CPU thread's CUDA context stack is
empty).
""",
CUcontext("ctx", "context to bind to the calling CPU thread")
)
IgnoreMissing..CUresult(
"CtxGetCurrent",
"""
Returns the CUDA context bound to the calling CPU thread.
Returns in {@code *pctx} the CUDA context bound to the calling CPU thread. If no context is bound to the calling CPU thread then {@code *pctx} is set
to #NULL and #CUDA_SUCCESS is returned.
""",
Check(1)..CUcontext.p("pctx", "returned context handle")
)
CUresult(
"CtxGetDevice",
"""
Returns the device ID for the current context.
Returns in {@code *device} the ordinal of the current context's device.
""",
Check(1)..CUdevice.p("device", "returned device ID for the current context")
)
IgnoreMissing..CUresult(
"CtxGetFlags",
"""
Returns the flags for the current context.
Returns in {@code *flags} the flags of the current context. See #CtxCreate() for flag values.
""",
Check(1)..unsigned_int.p("flags", "pointer to store flags of current context")
)
CUresult(
"CtxSynchronize",
"""
Block for a context's tasks to complete.
Blocks until the device has completed all preceding requested tasks. {@code cuCtxSynchronize()} returns an error if one of the preceding tasks failed.
If the context was created with the #CTX_SCHED_BLOCKING_SYNC flag, the CPU thread will block until the GPU context has finished its work.
""",
void()
)
CUresult(
"CtxSetLimit",
"""
Set resource limits.
Setting {@code limit} to {@code value} is a request by the application to update the current limit maintained by the context. The driver is free to
modify the requested value to meet h/w requirements (this could be clamping to minimum or maximum values, rounding up to nearest element size, etc).
The application can use #CtxGetLimit() to find out exactly what the limit has been set to.
Setting each {@code CUlimit} has its own specific restrictions, so each is discussed here.
${ul(
"""
#LIMIT_STACK_SIZE controls the stack size in bytes of each GPU thread. The driver automatically increases the per-thread stack size for each kernel
launch as needed. This size isn't reset back to the original value after each launch. Setting this value will take effect immediately, and if
necessary, the device will block until all preceding requested tasks are complete.
""",
"""
#LIMIT_PRINTF_FIFO_SIZE controls the size in bytes of the FIFO used by the {@code printf()} device system call. Setting #LIMIT_PRINTF_FIFO_SIZE
must be performed before launching any kernel that uses the {@code printf()} device system call, otherwise #CUDA_ERROR_INVALID_VALUE will be
returned.
""",
"""
#LIMIT_MALLOC_HEAP_SIZE controls the size in bytes of the heap used by the {@code malloc()} and {@code free()} device system calls. Setting
{@code CU_LIMIT_MALLOC_HEAP_SIZE} must be performed before launching any kernel that uses the {@code malloc()} or {@code free()} device system
calls, otherwise #CUDA_ERROR_INVALID_VALUE will be returned.
""",
"""
#LIMIT_DEV_RUNTIME_SYNC_DEPTH controls the maximum nesting depth of a grid at which a thread can safely call {@code cudaDeviceSynchronize()}.
Setting this limit must be performed before any launch of a kernel that uses the device runtime and calls {@code cudaDeviceSynchronize()} above the
default sync depth, two levels of grids. Calls to {@code cudaDeviceSynchronize()} will fail with error code {@code cudaErrorSyncDepthExceeded} if
the limitation is violated. This limit can be set smaller than the default or up the maximum launch depth of 24. When setting this limit, keep in
mind that additional levels of sync depth require the driver to reserve large amounts of device memory which can no longer be used for user
allocations. If these reservations of device memory fail, {@code cuCtxSetLimit()} will return #CUDA_ERROR_OUT_OF_MEMORY, and the limit can be reset
to a lower value. This limit is only applicable to devices of compute capability 3.5 and higher. Attempting to set this limit on devices of compute
capability less than 3.5 will result in the error #CUDA_ERROR_UNSUPPORTED_LIMIT being returned.
""",
"""
#LIMIT_DEV_RUNTIME_PENDING_LAUNCH_COUNT controls the maximum number of outstanding device runtime launches that can be made from the current
context. A grid is outstanding from the point of launch up until the grid is known to have been completed. Device runtime launches which violate
this limitation fail and return {@code cudaErrorLaunchPendingCountExceeded} when {@code cudaGetLastError()} is called after launch. If more pending
launches than the default (2048 launches) are needed for a module using the device runtime, this limit can be increased. Keep in mind that being
able to sustain additional pending launches will require the driver to reserve larger amounts of device memory upfront which can no longer be used
for allocations. If these reservations fail, {@code cuCtxSetLimit()} will return #CUDA_ERROR_OUT_OF_MEMORY, and the limit can be reset to a lower
value. This limit is only applicable to devices of compute capability 3.5 and higher. Attempting to set this limit on devices of compute capability
less than 3.5 will result in the error #CUDA_ERROR_UNSUPPORTED_LIMIT being returned.
""",
"""
#LIMIT_MAX_L2_FETCH_GRANULARITY controls the L2 cache fetch granularity. Values can range from 0B to 128B. This is purely a performence hint and it
can be ignored or clamped depending on the platform.
""",
"""
#LIMIT_PERSISTING_L2_CACHE_SIZE controls size in bytes availabe for persisting L2 cache. This is purely a performance hint and it can be ignored or
clamped depending on the platform.
"""
)}
""",
CUlimit("limit", "limit to set"),
size_t("value", "size of limit")
)
CUresult(
"CtxGetLimit",
"""
Returns resource limits.
Returns in {@code *pvalue} the current size of {@code limit}.
""",
Check(1)..size_t.p("pvalue", "returned size of limit"),
CUlimit("limit", "limit to query")
)
CUresult(
"CtxGetCacheConfig",
"""
Returns the preferred cache configuration for the current context.
On devices where the L1 cache and shared memory use the same hardware resources, this function returns through {@code pconfig} the preferred cache
configuration for the current context. This is only a preference. The driver will use the requested configuration if possible, but it is free to choose
a different configuration if required to execute functions.
This will return a {@code pconfig} of #FUNC_CACHE_PREFER_NONE on devices where the size of the L1 cache and shared memory are fixed.
""",
Check(1)..CUfunc_cache.p("pconfig", "returned cache configuration")
)
CUresult(
"CtxSetCacheConfig",
"""
Sets the preferred cache configuration for the current context.
On devices where the L1 cache and shared memory use the same hardware resources, this sets through {@code config} the preferred cache configuration for
the current context. This is only a preference. The driver will use the requested configuration if possible, but it is free to choose a different
configuration if required to execute the function. Any function preference set via {@code cuFuncSetCacheConfig()} will be preferred over this
context-wide setting. Setting the context-wide cache configuration to #FUNC_CACHE_PREFER_NONE will cause subsequent kernel launches to prefer to not
change the cache configuration unless required to launch the kernel.
This setting does nothing on devices where the size of the L1 cache and shared memory are fixed.
Launching a kernel with a different preference than the most recent preference setting may insert a device-side synchronization point.
""",
CUfunc_cache("config", "requested cache configuration")
)
IgnoreMissing..CUresult(
"CtxGetSharedMemConfig",
"""
Returns the current shared memory configuration for the current context.
This function will return in {@code pConfig} the current size of shared memory banks in the current context. On devices with configurable shared memory
banks, #CtxSetSharedMemConfig() can be used to change this setting, so that all subsequent kernel launches will by default use the new bank size.
When {@code cuCtxGetSharedMemConfig} is called on devices without configurable shared memory, it will return the fixed bank size of the hardware.
""",
Check(1)..CUsharedconfig.p("pConfig", "returned shared memory configuration")
)
IgnoreMissing..CUresult(
"CtxSetSharedMemConfig",
"""
Sets the shared memory configuration for the current context.
On devices with configurable shared memory banks, this function will set the context's shared memory bank size which is used for subsequent kernel
launches.
Changed the shared memory configuration between launches may insert a device side synchronization point between those launches.
Changing the shared memory bank size will not increase shared memory usage or affect occupancy of kernels, but may have major effects on performance.
Larger bank sizes will allow for greater potential bandwidth to shared memory, but will change what kinds of accesses to shared memory will result in
bank conflicts.
This function will do nothing on devices with fixed shared memory bank size.
""",
CUsharedconfig("config", "requested shared memory configuration")
)
CUresult(
"CtxGetApiVersion",
"""
Gets the context's API version.
Returns a version number in {@code version} corresponding to the capabilities of the context (e.g. 3010 or 3020), which library developers can use to
direct callers to a specific API version. If {@code ctx} is #NULL, returns the API version used to create the currently bound context.
Note that new API versions are only introduced when context capabilities are changed that break binary compatibility, so the API version and driver
version may be different. For example, it is valid for the API version to be 3020 while the driver version is 4020.
""",
nullable..CUcontext("ctx", "context to check"),
Check(1)..unsigned_int.p("version", "pointer to version")
)
CUresult(
"CtxGetStreamPriorityRange",
"""
Returns numerical values that correspond to the least and greatest stream priorities.
Returns in {@code *leastPriority} and {@code *greatestPriority} the numerical values that correspond to the least and greatest stream priorities
respectively. Stream priorities follow a convention where lower numbers imply greater priorities. The range of meaningful stream priorities is given by
[ {@code *greatestPriority}, {@code *leastPriority]}. If the user attempts to create a stream with a priority value that is outside the meaningful
range as specified by this API, the priority is automatically clamped down or up to either {@code *leastPriority} or {@code *greatestPriority}
respectively. See #StreamCreateWithPriority() for details on creating a priority stream. A #NULL may be passed in for {@code *leastPriority} or {@code
*greatestPriority} if the value is not desired.
This function will return {@code 0} in both {@code *leastPriority} and {@code *greatestPriority} if the current context's device does not support
stream priorities (see #DeviceGetAttribute()).
""",
Check(1)..nullable..int.p("leastPriority", "pointer to an int in which the numerical value for least stream priority is returned"),
Check(1)..nullable..int.p("greatestPriority", "pointer to an int in which the numerical value for greatest stream priority is returned")
)
IgnoreMissing..CUresult(
"CtxResetPersistingL2Cache",
"""
Resets all persisting lines in cache to normal status.
Takes effect on function return.
""",
void()
)
IgnoreMissing..CUresult(
"CtxGetExecAffinity",
"""
Returns the execution affinity setting for the current context.
Returns in {@code *pExecAffinity} the current value of {@code type}.
""",
Check(1)..CUexecAffinityParam.p("pExecAffinity", "returned execution affinity"),
CUexecAffinityType("type", "execution affinity type to query")
)
CUresult(
"CtxAttach",
"""
Increment a context's usage-count.
Deprecated: Note that this function is deprecated and should not be used.
Increments the usage count of the context and passes back a context handle in {@code *pctx} that must be passed to #CtxDetach() when the application
is done with the context. {@code cuCtxAttach()} fails if there is no context current to the thread.
Currently, the {@code flags} parameter must be 0.
""",
Check(1)..CUcontext.p("pctx", "returned context handle of the current context"),
unsigned_int("flags", "context attach flags (must be 0)")
)
CUresult(
"CtxDetach",
"""
Decrement a context's usage-count
Deprecated: Note that this function is deprecated and should not be used.
Decrements the usage count of the context {@code ctx}, and destroys the context if the usage count goes to 0. The context must be a handle that was
passed back by #CtxCreate() or #CtxAttach(), and must be current to the calling thread.
""",
CUcontext("ctx", "context to destroy")
)
CUresult(
"ModuleLoad",
"""
Loads a compute module.
Takes a filename {@code fname} and loads the corresponding module {@code module} into the current context. The CUDA driver API does not attempt to
lazily allocate the resources needed by a module; if the memory for functions and data (constant and global) needed by the module cannot be allocated,
{@code cuModuleLoad()} fails. The file should be a <em>cubin</em> file as output by <b>nvcc</b>, or a <em>PTX</em> file either as output by <b>nvcc</b>
or handwritten, or a <em>fatbin</em> file as output by <b>nvcc</b> from toolchain 4.0 or later.
""",
Check(1)..CUmodule.p("module", "returned module"),
charUTF8.const.p("fname", "filename of module to load")
)
CUresult(
"ModuleLoadData",
"""
Load a module's data.
Takes a pointer {@code image} and loads the corresponding module {@code module} into the current context. The pointer may be obtained by mapping a
<em>cubin</em> or <em>PTX</em> or <em>fatbin</em> file, passing a <em>cubin</em> or <em>PTX</em> or <em>fatbin</em> file as a NULL-terminated text
string, or incorporating a <em>cubin</em> or <em>fatbin</em> object into the executable resources and using operating system calls such as Windows
{@code FindResource()} to obtain the pointer.
""",
Check(1)..CUmodule.p("module", "returned module"),
Unsafe..void.const.p("image", "module data to load")
)
CUresult(
"ModuleLoadDataEx",
"""
Load a module's data with options.
Takes a pointer {@code image} and loads the corresponding module {@code module} into the current context. The pointer may be obtained by mapping a
<em>cubin</em> or <em>PTX</em> or <em>fatbin</em> file, passing a <em>cubin</em> or <em>PTX</em> or <em>fatbin</em> file as a NULL-terminated text
string, or incorporating a <em>cubin</em> or <em>fatbin</em> object into the executable resources and using operating system calls such as Windows
{@code FindResource()} to obtain the pointer. Options are passed as an array via {@code options} and any corresponding parameters are passed in {@code
optionValues}. The number of total options is supplied via {@code numOptions}. Any outputs will be returned via {@code optionValues}.
""",
Check(1)..CUmodule.p("module", "returned module"),
Unsafe..void.const.p("image", "module data to load"),
AutoSize("options", "optionValues")..unsigned_int("numOptions", "number of options"),
nullable..CUjit_option.p("options", "options for JIT"),
nullable..void.p.p("optionValues", "option values for JIT")
)
CUresult(
"ModuleLoadFatBinary",
"""
Load a module's data.
Takes a pointer {@code fatCubin} and loads the corresponding module {@code module} into the current context. The pointer represents a <i>fat binary</i>
object, which is a collection of different <em>cubin</em> and/or <em>PTX</em> files, all representing the same device code, but compiled and optimized
for different architectures.
Prior to CUDA 4.0, there was no documented API for constructing and using fat binary objects by programmers. Starting with CUDA 4.0, fat binary objects
can be constructed by providing the <i>-fatbin option</i> to <b>nvcc</b>. More information can be found in the <b>nvcc</b> document.
""",
Check(1)..CUmodule.p("module", "returned module"),
Unsafe..void.const.p("fatCubin", "fat binary to load")
)
CUresult(
"ModuleUnload",
"""
Unloads a module.
Unloads a module {@code hmod} from the current context.
""",
CUmodule("hmod", "module to unload")
)
CUresult(
"ModuleGetFunction",
"""
Returns a function handle.
Returns in {@code *hfunc} the handle of the function of name {@code name} located in module {@code hmod}. If no function of that name exists,
{@code cuModuleGetFunction()} returns #CUDA_ERROR_NOT_FOUND.
""",
Check(1)..CUfunction.p("hfunc", "returned function handle"),
CUmodule("hmod", "module to retrieve function from"),
charUTF8.const.p("name", "name of function to retrieve")
)
CUresult(
"ModuleGetGlobal",
"""
Returns a global pointer from a module.
Returns in {@code *dptr} and {@code *bytes} the base pointer and size of the global of name {@code name} located in module {@code hmod}. If no variable
of that name exists, {@code cuModuleGetGlobal()} returns #CUDA_ERROR_NOT_FOUND. Both parameters {@code dptr} and {@code bytes} are optional. If one of
them is #NULL, it is ignored.
""",
Check(1)..nullable..CUdeviceptr.p("dptr", "returned global device pointer"),
Check(1)..nullable..size_t.p("bytes", "returned global size in bytes"),
CUmodule("hmod", "module to retrieve global from"),
charUTF8.const.p("name", "name of global to retrieve")
).versioned()
CUresult(
"ModuleGetTexRef",
"""
Returns a handle to a texture reference.
Returns in {@code *pTexRef} the handle of the texture reference of name {@code name} in the module {@code hmod}. If no texture reference of that name
exists, {@code cuModuleGetTexRef()} returns #CUDA_ERROR_NOT_FOUND. This texture reference handle should not be destroyed, since it will be destroyed
when the module is unloaded.
""",
Check(1)..CUtexref.p("pTexRef", "returned texture reference"),
CUmodule("hmod", "module to retrieve texture reference from"),
charUTF8.const.p("name", "name of texture reference to retrieve")
)
CUresult(
"ModuleGetSurfRef",
"""
Returns a handle to a surface reference.
Returns in {@code *pSurfRef} the handle of the surface reference of name {@code name} in the module {@code hmod}. If no surface reference of that name
exists, {@code cuModuleGetSurfRef()} returns #CUDA_ERROR_NOT_FOUND.
""",
Check(1)..CUsurfref.p("pSurfRef", "returned surface reference"),
CUmodule("hmod", "module to retrieve surface reference from"),
charUTF8.const.p("name", "name of surface reference to retrieve")
)
IgnoreMissing..CUresult(
"LinkCreate",
"""
Creates a pending JIT linker invocation.
If the call is successful, the caller owns the returned {@code CUlinkState}, which should eventually be destroyed with #LinkDestroy(). The device code
machine size (32 or 64 bit) will match the calling application.
Both linker and compiler options may be specified. Compiler options will be applied to inputs to this linker action which must be compiled from PTX.
The options #JIT_WALL_TIME, #JIT_INFO_LOG_BUFFER_SIZE_BYTES, and #JIT_ERROR_LOG_BUFFER_SIZE_BYTES will accumulate data until the {@code CUlinkState} is
destroyed.
{@code optionValues} must remain valid for the life of the {@code CUlinkState} if output options are used. No other references to inputs are maintained
after this call returns.
""",
AutoSize("options", "optionValues")..unsigned_int("numOptions", "size of options arrays"),
CUjit_option.p("options", "array of linker and compiler options"),
void.p.p("optionValues", "array of option values, each cast to void *"),
Check(1)..CUlinkState.p("stateOut", "on success, this will contain a {@code CUlinkState} to specify and complete this action")
).versioned()
IgnoreMissing..CUresult(
"LinkAddData",
"""
Add an input to a pending linker invocation.
Ownership of {@code data} is retained by the caller. No reference is retained to any inputs after this call returns.
This method accepts only compiler options, which are used if the data must be compiled from PTX, and does not accept any of #JIT_WALL_TIME,
#JIT_INFO_LOG_BUFFER, #JIT_ERROR_LOG_BUFFER, #JIT_TARGET_FROM_CUCONTEXT, or #JIT_TARGET.
""",
CUlinkState("state", "a pending linker action"),
CUjitInputType("type", "the type of the input data"),
void.p("data", "the input data. PTX must be NULL-terminated."),
AutoSize("data")..size_t("size", "the length of the input data"),
charUTF8.const.p("name", "an optional name for this input in log messages"),
AutoSize("options", "optionValues")..unsigned_int("numOptions", "size of options"),
CUjit_option.p("options", "options to be applied only for this input (overrides options from #LinkCreate())"),
void.p.p("optionValues", "array of option values, each cast to void *")
).versioned()
IgnoreMissing..CUresult(
"LinkAddFile",
"""
Add a file input to a pending linker invocation.
No reference is retained to any inputs after this call returns.
This method accepts only compiler options, which are used if the input must be compiled from PTX, and does not accept any of #JIT_WALL_TIME,
#JIT_INFO_LOG_BUFFER, #JIT_ERROR_LOG_BUFFER, #JIT_TARGET_FROM_CUCONTEXT, or #JIT_TARGET.
This method is equivalent to invoking #LinkAddData() on the contents of the file.
""",
CUlinkState("state", "a pending linker action"),
CUjitInputType("type", "the type of the input data"),
charUTF8.const.p("path", "path to the input file"),
AutoSize("options", "optionValues")..unsigned_int("numOptions", "size of options"),
CUjit_option.p("options", "options to be applied only for this input (overrides options from #LinkCreate())"),
void.p.p("optionValues", "array of option values, each cast to void *")
).versioned()
IgnoreMissing..CUresult(
"LinkComplete",
"""
Complete a pending linker invocation.
Completes the pending linker action and returns the cubin image for the linked device code, which can be used with #ModuleLoadData(). The cubin is
owned by {@code state}, so it should be loaded before {@code state} is destroyed via #LinkDestroy(). This call does not destroy {@code state}.
""",
CUlinkState("state", "a pending linker invocation"),
Check(1)..void.p.p("cubinOut", "on success, this will point to the output image"),
Check(1)..size_t.p("sizeOut", "optional parameter to receive the size of the generated image")
)
IgnoreMissing..CUresult(
"LinkDestroy",
"Destroys state for a JIT linker invocation.",
CUlinkState("state", "state object for the linker invocation")
)
CUresult(
"MemGetInfo",
"""
Gets free and total memory.
Returns in {@code *total} the total amount of memory available to the the current context. Returns in {@code *free} the amount of memory on the device
that is free according to the OS. CUDA is not guaranteed to be able to allocate all of the memory that the OS reports as free.
""",
Check(1)..size_t.p("free", "returned free memory in bytes"),
Check(1)..size_t.p("total", "returned total memory in bytes")
).versioned()
CUresult(
"MemAlloc",
"""
Allocates device memory.
Allocates {@code bytesize} bytes of linear memory on the device and returns in {@code *dptr} a pointer to the allocated memory. The allocated memory is
suitably aligned for any kind of variable. The memory is not cleared. If {@code bytesize} is 0, {@code cuMemAlloc()} returns #CUDA_ERROR_INVALID_VALUE.
""",
Check(1)..CUdeviceptr.p("dptr", "returned device pointer"),
size_t("bytesize", "requested allocation size in bytes")
).versioned()
CUresult(
"MemAllocPitch",
"""
Allocates pitched device memory.
Allocates at least {@code WidthInBytes} * {@code Height} bytes of linear memory on the device and returns in {@code *dptr} a pointer to the allocated
memory. The function may pad the allocation to ensure that corresponding pointers in any given row will continue to meet the alignment requirements for
coalescing as the address is updated from row to row. {@code ElementSizeBytes} specifies the size of the largest reads and writes that will be
performed on the memory range. {@code ElementSizeBytes} may be 4, 8 or 16 (since coalesced memory transactions are not possible on other data sizes).
If {@code ElementSizeBytes} is smaller than the actual read/write size of a kernel, the kernel will run correctly, but possibly at reduced speed. The
pitch returned in {@code *pPitch} by {@code cuMemAllocPitch()} is the width in bytes of the allocation. The intended usage of pitch is as a separate
parameter of the allocation, used to compute addresses within the 2D array. Given the row and column of an array element of type <b>T</b>, the address
is computed as:
${codeBlock("""
T* pElement = (T*)((char*)BaseAddress + Row * Pitch) + Column;""")}
The pitch returned by {@code cuMemAllocPitch()} is guaranteed to work with #Memcpy2D() under all circumstances. For allocations of 2D arrays, it is
recommended that programmers consider performing pitch allocations using {@code cuMemAllocPitch()}. Due to alignment restrictions in the hardware, this
is especially true if the application will be performing 2D memory copies between different regions of device memory (whether linear memory or CUDA
arrays).
The byte alignment of the pitch returned by {@code cuMemAllocPitch()} is guaranteed to match or exceed the alignment requirement for texture binding
with #TexRefSetAddress2D().
""",
Check(1)..CUdeviceptr.p("dptr", "returned device pointer"),
Check(1)..size_t.p("pPitch", "returned pitch of allocation in bytes"),
size_t("WidthInBytes", "requested allocation width in bytes"),
size_t("Height", "requested allocation height in rows"),
unsigned_int("ElementSizeBytes", "size of largest reads/writes for range")
).versioned()
CUresult(
"MemFree",
"""
Frees device memory.
Frees the memory space pointed to by {@code dptr}, which must have been returned by a previous call to #MemAlloc() or #MemAllocPitch().
""",
CUdeviceptr("dptr", "pointer to memory to free")
).versioned()
CUresult(
"MemGetAddressRange",
"""
Get information on memory allocations.
Returns the base address in {@code *pbase} and size in {@code *psize} of the allocation by #MemAlloc() or #MemAllocPitch() that contains the input
pointer {@code dptr}. Both parameters {@code pbase} and {@code psize} are optional. If one of them is #NULL, it is ignored.
""",
Check(1)..nullable..CUdeviceptr.p("pbase", "returned base address"),
Check(1)..nullable..size_t.p("psize", "returned size of device memory allocation"),
CUdeviceptr("dptr", "device pointer to query")
).versioned()
CUresult(
"MemAllocHost",
"""
Allocates page-locked host memory.
Allocates {@code bytesize} bytes of host memory that is page-locked and accessible to the device. The driver tracks the virtual memory ranges allocated
with this function and automatically accelerates calls to functions such as #Memcpy(). Since the memory can be accessed directly by the device, it can
be read or written with much higher bandwidth than pageable memory obtained with functions such as {@code malloc()}. Allocating excessive amounts of
memory with {@code cuMemAllocHost()} may degrade system performance, since it reduces the amount of memory available to the system for paging. As a
result, this function is best used sparingly to allocate staging areas for data exchange between host and device.
Note all host memory allocated using {@code cuMemHostAlloc()} will automatically be immediately accessible to all contexts on all devices which support
unified addressing (as may be queried using #DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING). The device pointer that may be used to access this host memory from
those contexts is always equal to the returned host pointer {@code *pp}. See {@code CUDA_UNIFIED} for additional details.
""",
Check(1)..void.p.p("pp", "returned host pointer to page-locked memory"),
size_t("bytesize", "requested allocation size in bytes")
).versioned()
CUresult(
"MemFreeHost",
"""
Frees page-locked host memory.
Frees the memory space pointed to by {@code p}, which must have been returned by a previous call to #MemAllocHost().
""",
Unsafe..void.p("p", "pointer to memory to free")
)
CUresult(
"MemHostAlloc",
"""
Allocates page-locked host memory.
Allocates {@code bytesize} bytes of host memory that is page-locked and accessible to the device. The driver tracks the virtual memory ranges allocated
with this function and automatically accelerates calls to functions such as #MemcpyHtoD(). Since the memory can be accessed directly by the device,
it can be read or written with much higher bandwidth than pageable memory obtained with functions such as {@code malloc()}. Allocating excessive
amounts of pinned memory may degrade system performance, since it reduces the amount of memory available to the system for paging. As a result, this
function is best used sparingly to allocate staging areas for data exchange between host and device.
The {@code Flags} parameter enables different options to be specified that affect the allocation, as follows:
${ul(
"""
#MEMHOSTALLOC_PORTABLE: The memory returned by this call will be considered as pinned memory by all CUDA contexts, not just the one that performed
the allocation.
""",
"""
#MEMHOSTALLOC_DEVICEMAP: Maps the allocation into the CUDA address space. The device pointer to the memory may be obtained by calling
#MemHostGetDevicePointer().
""",
"""
#MEMHOSTALLOC_WRITECOMBINED: Allocates the memory as write-combined (WC). WC memory can be transferred across the PCI Express bus more quickly on
some system configurations, but cannot be read efficiently by most CPUs. WC memory is a good option for buffers that will be written by the CPU and
read by the GPU via mapped pinned memory or host->device transfers.
"""
)}
All of these flags are orthogonal to one another: a developer may allocate memory that is portable, mapped and/or write-combined with no restrictions.
The #MEMHOSTALLOC_DEVICEMAP flag may be specified on CUDA contexts for devices that do not support mapped pinned memory. The failure is deferred to
#MemHostGetDevicePointer() because the memory may be mapped into other CUDA contexts via the #MEMHOSTALLOC_PORTABLE flag.
The memory allocated by this function must be freed with #MemFreeHost().
Note all host memory allocated using {@code cuMemHostAlloc()} will automatically be immediately accessible to all contexts on all devices which support
unified addressing (as may be queried using #DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING). Unless the flag #MEMHOSTALLOC_WRITECOMBINED is specified, the device
pointer that may be used to access this host memory from those contexts is always equal to the returned host pointer {@code *pp}. If the flag
#MEMHOSTALLOC_WRITECOMBINED is specified, then the function #MemHostGetDevicePointer() must be used to query the device pointer, even if the context
supports unified addressing. See {@code CUDA_UNIFIED} for additional details.
""",
Check(1)..void.p.p("pp", "returned host pointer to page-locked memory"),
size_t("bytesize", "requested allocation size in bytes"),
unsigned_int("Flags", "flags for allocation request")
)
CUresult(
"MemHostGetDevicePointer",
"""
Passes back device pointer of mapped pinned memory.
Passes back the device pointer {@code pdptr} corresponding to the mapped, pinned host buffer {@code p} allocated by #MemHostAlloc().
{@code cuMemHostGetDevicePointer()} will fail if the #MEMHOSTALLOC_DEVICEMAP flag was not specified at the time the memory was allocated, or if the
function is called on a GPU that does not support mapped pinned memory.
For devices that have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_CAN_USE_HOST_POINTER_FOR_REGISTERED_MEM, the memory can also be
accessed from the device using the host pointer {@code p}. The device pointer returned by {@code cuMemHostGetDevicePointer()} may or may not match the
original host pointer {@code p} and depends on the devices visible to the application. If all devices visible to the application have a non-zero value
for the device attribute, the device pointer returned by {@code cuMemHostGetDevicePointer()} will match the original pointer {@code p}. If any device
visible to the application has a zero value for the device attribute, the device pointer returned by {@code cuMemHostGetDevicePointer()} will not match
the original host pointer {@code p}, but it will be suitable for use on all devices provided Unified Virtual Addressing is enabled. In such systems, it
is valid to access the memory using either pointer on devices that have a non-zero value for the device attribute. Note however that such devices
should access the memory using only of the two pointers and not both.
{@code Flags} provides for future releases. For now, it must be set to 0.
""",
Check(1)..CUdeviceptr.p("pdptr", "returned device pointer"),
Unsafe..void.p("p", "host pointer"),
unsigned_int("Flags", "options (must be 0)")
).versioned()
CUresult(
"MemHostGetFlags",
"""
Passes back flags that were used for a pinned allocation
Passes back the flags {@code pFlags} that were specified when allocating the pinned host buffer {@code p} allocated by #MemHostAlloc().
{@code cuMemHostGetFlags()} will fail if the pointer does not reside in an allocation performed by #MemAllocHost() or {@code cuMemHostAlloc()}.
""",
Check(1)..unsigned_int.p("pFlags", "returned flags word"),
Unsafe..void.p("p", "host pointer")
)
IgnoreMissing..CUresult(
"MemAllocManaged",
"""
Allocates memory that will be automatically managed by the Unified Memory system.
Allocates {@code bytesize} bytes of managed memory on the device and returns in {@code *dptr} a pointer to the allocated memory. If the device doesn't
support allocating managed memory, #CUDA_ERROR_NOT_SUPPORTED is returned. Support for managed memory can be queried using the device attribute
#DEVICE_ATTRIBUTE_MANAGED_MEMORY. The allocated memory is suitably aligned for any kind of variable. The memory is not cleared. If {@code bytesize}
is 0, #MemAllocManaged() returns #CUDA_ERROR_INVALID_VALUE. The pointer is valid on the CPU and on all GPUs in the system that support managed memory.
All accesses to this pointer must obey the Unified Memory programming model.
{@code flags} specifies the default stream association for this allocation. {@code flags} must be one of #MEM_ATTACH_GLOBAL or #MEM_ATTACH_HOST. If
#MEM_ATTACH_GLOBAL is specified, then this memory is accessible from any stream on any device. If #MEM_ATTACH_HOST is specified, then the allocation
should not be accessed from devices that have a zero value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS; an explicit call to
#StreamAttachMemAsync() will be required to enable access on such devices.
If the association is later changed via #StreamAttachMemAsync() to a single stream, the default association as specifed during #MemAllocManaged() is
restored when that stream is destroyed. For __managed__ variables, the default association is always #MEM_ATTACH_GLOBAL. Note that destroying a stream
is an asynchronous operation, and as a result, the change to default association won't happen until all work in the stream has completed.
Memory allocated with #MemAllocManaged() should be released with #MemFree().
Device memory oversubscription is possible for GPUs that have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS.
Managed memory on such GPUs may be evicted from device memory to host memory at any time by the Unified Memory driver in order to make room for other
allocations.
In a multi-GPU system where all GPUs have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, managed memory may
not be populated when this API returns and instead may be populated on access. In such systems, managed memory can migrate to any processor's memory at
any time. The Unified Memory driver will employ heuristics to maintain data locality and prevent excessive page faults to the extent possible. The
application can also guide the driver about memory usage patterns via #MemAdvise(). The application can also explicitly migrate memory to a desired
processor's memory via #MemPrefetchAsync().
In a multi-GPU system where all of the GPUs have a zero value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS and all the GPUs
have peer-to-peer support with each other, the physical storage for managed memory is created on the GPU which is active at the time
#MemAllocManaged() is called. All other GPUs will reference the data at reduced bandwidth via peer mappings over the PCIe bus. The Unified Memory
driver does not migrate memory among such GPUs.
In a multi-GPU system where not all GPUs have peer-to-peer support with each other and where the value of the device attribute
#DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS is zero for at least one of those GPUs, the location chosen for physical storage of managed memory is
system-dependent.
${ul(
"""
On Linux, the location chosen will be device memory as long as the current set of active contexts are on devices that either have peer-to-peer
support with each other or have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS. If there is an active
context on a GPU that does not have a non-zero value for that device attribute and it does not have peer-to-peer support with the other devices
that have active contexts on them, then the location for physical storage will be 'zero-copy' or host memory. Note that this means that managed
memory that is located in device memory is migrated to host memory if a new context is created on a GPU that doesn't have a non-zero value for the
device attribute and does not support peer-to-peer with at least one of the other devices that has an active context. This in turn implies that
context creation may fail if there is insufficient host memory to migrate all managed allocations.
""",
"""
On Windows, the physical storage is always created in 'zero-copy' or host memory. All GPUs will reference the data at reduced bandwidth over the
PCIe bus. In these circumstances, use of the environment variable {@code CUDA_VISIBLE_DEVICES} is recommended to restrict CUDA to only use those
GPUs that have peer-to-peer support. Alternatively, users can also set {@code CUDA_MANAGED_FORCE_DEVICE_ALLOC} to a non-zero value to force the
driver to always use device memory for physical storage. When this environment variable is set to a non-zero value, all contexts created in that
process on devices that support managed memory have to be peer-to-peer compatible with each other. Context creation will fail if a context is
created on a device that supports managed memory and is not peer-to-peer compatible with any of the other managed memory supporting devices on
which contexts were previously created, even if those contexts have been destroyed. These environment variables are described in the CUDA
programming guide under the "CUDA environment variables" section.
""",
"On ARM, managed memory is not available on discrete gpu with Drive PX-2."
)}
""",
Check(1)..CUdeviceptr.p("dptr", "returned device pointer"),
size_t("bytesize", "requested allocation size in bytes"),
unsigned_int("flags", "must be one of #MEM_ATTACH_GLOBAL or #MEM_ATTACH_HOST")
)
IgnoreMissing..CUresult(
"DeviceGetByPCIBusId",
"""
Returns a handle to a compute device.
Returns in {@code *device} a device handle given a PCI bus ID string.
""",
Check(1)..CUdevice.p("dev", "returned device handle"),
charASCII.const.p(
"pciBusId",
"""
string in one of the following forms: {@code [domain]:[bus]:[device].[function] [domain]:[bus]:[device] [bus]:[device].[function]} where
{@code domain}, {@code bus}, {@code device}, and {@code function} are all hexadecimal values
"""
)
)
IgnoreMissing..CUresult(
"DeviceGetPCIBusId",
"""
Returns a PCI Bus Id string for the device.
Returns an ASCII string identifying the device {@code dev} in the NULL-terminated string pointed to by {@code pciBusId}. {@code len} specifies the
maximum length of the string that may be returned.
""",
char.p(
"pciBusId",
"""
returned identifier string for the device in the following format {@code [domain]:[bus]:[device].[function]} where {@code domain}, {@code bus},
{@code device}, and {@code function} are all hexadecimal values. {@code pciBusId} should be large enough to store 13 characters including the
NULL-terminator.
"""
),
AutoSize("pciBusId")..int("len", "maximum length of string to store in {@code name}"),
CUdevice("dev", "device to get identifier string for")
)
IgnoreMissing..CUresult(
"IpcGetEventHandle",
"""
Gets an interprocess handle for a previously allocated event.
Takes as input a previously allocated event. This event must have been created with the #EVENT_INTERPROCESS and #EVENT_DISABLE_TIMING flags set. This
opaque handle may be copied into other processes and opened with {@link \#cuIpcOpenEventHandle IpcOpenEventHandle} to allow efficient hardware
synchronization between GPU work in different processes.
After the event has been opened in the importing process, #EventRecord(), #EventSynchronize(), #StreamWaitEvent() and #EventQuery() may be used in
either process. Performing operations on the imported event after the exported event has been freed with #EventDestroy() will result in undefined
behavior.
IPC functionality is restricted to devices with support for unified addressing on Linux and Windows operating systems. IPC functionality on Windows is
restricted to GPUs in TCC mode.
""",
CUipcEventHandle.p("pHandle", "pointer to a user allocated {@code CUipcEventHandle} in which to return the opaque event handle"),
CUevent("event", "event allocated with #EVENT_INTERPROCESS and #EVENT_DISABLE_TIMING flags")
)
IgnoreMissing..CUresult(
"IpcOpenEventHandle",
"""
Opens an interprocess event handle for use in the current process.
Opens an interprocess event handle exported from another process with #IpcGetEventHandle(). This function returns a {@code CUevent} that behaves like a
locally created event with the #EVENT_DISABLE_TIMING flag specified. This event must be freed with #EventDestroy().
Performing operations on the imported event after the exported event has been freed with {@code cuEventDestroy} will result in undefined behavior.
IPC functionality is restricted to devices with support for unified addressing on Linux and Windows operating systems. IPC functionality on Windows is
restricted to GPUs in TCC mode.
""",
Check(1)..CUevent.p("phEvent", "returns the imported event"),
CUipcEventHandle("handle", "interprocess handle to open")
)
IgnoreMissing..CUresult(
"IpcGetMemHandle",
"""
Gets an interprocess memory handle for an existing device memory allocation.
Takes a pointer to the base of an existing device memory allocation created with #MemAlloc() and exports it for use in another process. This is a
lightweight operation and may be called multiple times on an allocation without adverse effects.
If a region of memory is freed with #MemFree() and a subsequent call to #MemAlloc() returns memory with the same device address, #IpcGetMemHandle()
will return a unique handle for the new memory.
IPC functionality is restricted to devices with support for unified addressing on Linux and Windows operating systems. IPC functionality on Windows is
restricted to GPUs in TCC mode.
""",
CUipcMemHandle.p("pHandle", "pointer to user allocated {@code CUipcMemHandle} to return the handle in"),
CUdeviceptr("dptr", "base pointer to previously allocated device memory")
)
IgnoreMissing..CUresult(
"IpcOpenMemHandle",
"""
Opens an interprocess memory handle exported from another process and returns a device pointer usable in the local process.
Maps memory exported from another process with #IpcGetMemHandle() into the current device address space. For contexts on different devices
{@code cuIpcOpenMemHandle} can attempt to enable peer access between the devices as if the user called #CtxEnablePeerAccess(). This behavior is
controlled by the #IPC_MEM_LAZY_ENABLE_PEER_ACCESS flag. #DeviceCanAccessPeer() can determine if a mapping is possible.
Contexts that may open ##CUIPCMemHandle are restricted in the following way. {@code CUipcMemHandle}s from each {@code CUdevice} in a given process may
only be opened by one {@code CUcontext} per {@code CUdevice} per other process.
If the memory handle has already been opened by the current context, the reference count on the handle is incremented by 1 and the existing device
pointer is returned.
Memory returned from {@code cuIpcOpenMemHandle} must be freed with #IpcCloseMemHandle().
Calling #MemFree() on an exported memory region before calling {@code cuIpcCloseMemHandle} in the importing context will result in undefined behavior.
IPC functionality is restricted to devices with support for unified addressing on Linux and Windows operating systems. IPC functionality on Windows is
restricted to GPUs in TCC mode.
${note("""
No guarantees are made about the address returned in {@code *pdptr}. In particular, multiple processes may not receive the same address for the same
{@code handle}.""")}
""",
Check(1)..CUdeviceptr.p("pdptr", "returned device pointer"),
CUipcMemHandle("handle", "{@code CUipcMemHandle} to open"),
unsigned_int("Flags", "flags for this operation. Must be specified as #IPC_MEM_LAZY_ENABLE_PEER_ACCESS.")
).versioned()
IgnoreMissing..CUresult(
"IpcCloseMemHandle",
"""
Attempts to close memory mapped with {@link \#cuIpcOpenMemHandle IpcOpenMemHandle}.
Decrements the reference count of the memory returned by {@code cuIpcOpenMemHandle()} by 1. When the reference count reaches 0, this API unmaps the
memory. The original allocation in the exporting process as well as imported mappings in other processes will be unaffected.
Any resources used to enable peer access will be freed if this is the last mapping using them.
IPC functionality is restricted to devices with support for unified addressing on Linux and Windows operating systems. IPC functionality on Windows is
restricted to GPUs in TCC mode
""",
CUdeviceptr("dptr", "device pointer returned by {@code cuIpcOpenMemHandle()}")
)
IgnoreMissing..CUresult(
"MemHostRegister",
"""
Registers an existing host memory range for use by CUDA.
Page-locks the memory range specified by {@code p} and {@code bytesize} and maps it for the device(s) as specified by {@code Flags}. This memory range
also is added to the same tracking mechanism as #MemHostAlloc() to automatically accelerate calls to functions such as #MemcpyHtoD(). Since the memory
can be accessed directly by the device, it can be read or written with much higher bandwidth than pageable memory that has not been registered.
Page-locking excessive amounts of memory may degrade system performance, since it reduces the amount of memory available to the system for paging. As a
result, this function is best used sparingly to register staging areas for data exchange between host and device.
This function has limited support on Mac OS X. OS 10.7 or higher is required.
All flags are orthogonal to one another: a developer may page-lock memory that is portable or mapped with no restrictions.
The #MEMHOSTREGISTER_DEVICEMAP flag may be specified on CUDA contexts for devices that do not support mapped pinned memory. The failure is deferred to
#MemHostGetDevicePointer() because the memory may be mapped into other CUDA contexts via the #MEMHOSTREGISTER_PORTABLE flag.
For devices that have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_CAN_USE_HOST_POINTER_FOR_REGISTERED_MEM, the memory can also be
accessed from the device using the host pointer {@code p}. The device pointer returned by {@code cuMemHostGetDevicePointer()} may or may not match the
original host pointer {@code ptr} and depends on the devices visible to the application. If all devices visible to the application have a non-zero
value for the device attribute, the device pointer returned by {@code cuMemHostGetDevicePointer()} will match the original pointer {@code ptr}. If any
device visible to the application has a zero value for the device attribute, the device pointer returned by {@code cuMemHostGetDevicePointer()} will
not match the original host pointer {@code ptr}, but it will be suitable for use on all devices provided Unified Virtual Addressing is enabled. In such
systems, it is valid to access the memory using either pointer on devices that have a non-zero value for the device attribute. Note however that such
devices should access the memory using only of the two pointers and not both.
The memory page-locked by this function must be unregistered with #MemHostUnregister().
""",
void.p("p", "host pointer to memory to page-lock"),
AutoSize("p")..size_t("bytesize", "size in bytes of the address range to page-lock"),
unsigned_int("Flags", "flags for allocation request", "MEMHOSTREGISTER_\\w+", LinkMode.BITFIELD)
).versioned()
IgnoreMissing..CUresult(
"MemHostUnregister",
"""
Unregisters a memory range that was registered with #MemHostRegister().
Unmaps the memory range whose base address is specified by {@code p}, and makes it pageable again.
The base address must be the same one specified to #MemHostRegister().
""",
Unsafe..void.p("p", "host pointer to memory to unregister")
)
IgnoreMissing..CUresult(
"Memcpy",
"""
Copies memory.
Copies data between two pointers. {@code dst} and {@code src} are base pointers of the destination and source, respectively. {@code ByteCount}
specifies the number of bytes to copy. Note that this function infers the type of the transfer (host to host, host to device, device to device, or
device to host) from the pointer values. This function is only allowed in contexts which support unified addressing.
""",
CUdeviceptr("dst", "destination unified virtual address space pointer"),
CUdeviceptr("src", "source unified virtual address space pointer"),
size_t("ByteCount", "size of memory copy in bytes")
).ptds()
IgnoreMissing..CUresult(
"MemcpyPeer",
"""
Copies device memory between two contexts.
Copies from device memory in one context to device memory in another context. {@code dstDevice} is the base device pointer of the destination memory
and {@code dstContext} is the destination context. {@code srcDevice} is the base device pointer of the source memory and {@code srcContext} is the
source pointer. {@code ByteCount} specifies the number of bytes to copy.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
CUcontext("dstContext", "destination context"),
CUdeviceptr("srcDevice", "source device pointer"),
CUcontext("srcContext", "source context"),
size_t("ByteCount", "size of memory copy in bytes")
).ptds()
CUresult(
"MemcpyHtoD",
"""
Copies memory from Host to Device.
Copies from host memory to device memory. {@code dstDevice} and {@code srcHost} are the base addresses of the destination and source, respectively.
{@code ByteCount} specifies the number of bytes to copy.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
MultiTypeAll..void.const.p("srcHost", "source host pointer"),
AutoSize("srcHost")..size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyDtoH",
"""
Copies memory from Device to Host.
Copies from device to host memory. {@code dstHost} and {@code srcDevice} specify the base pointers of the destination and source, respectively. {@code
ByteCount} specifies the number of bytes to copy.
""",
MultiTypeAll..void.p("dstHost", "destination host pointer"),
CUdeviceptr("srcDevice", "source device pointer"),
AutoSize("dstHost")..size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyDtoD",
"""
Copies memory from Device to Device.
Copies from device memory to device memory. {@code dstDevice} and {@code srcDevice} are the base pointers of the destination and source, respectively.
{@code ByteCount} specifies the number of bytes to copy.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
CUdeviceptr("srcDevice", "source device pointer"),
size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyDtoA",
"""
Copies memory from Device to Array.
Copies from device memory to a 1D CUDA array. {@code dstArray} and {@code dstOffset} specify the CUDA array handle and starting index of the
destination data. {@code srcDevice} specifies the base pointer of the source. {@code ByteCount} specifies the number of bytes to copy.
""",
CUarray("dstArray", "destination array"),
size_t("dstOffset", "offset in bytes of destination array"),
CUdeviceptr("srcDevice", "source device pointer"),
size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyAtoD",
"""
Copies memory from Array to Device.
Copies from one 1D CUDA array to device memory. {@code dstDevice} specifies the base pointer of the destination and must be naturally aligned with the
CUDA array elements. {@code srcArray} and {@code srcOffset} specify the CUDA array handle and the offset in bytes into the array where the copy is to
begin. {@code ByteCount} specifies the number of bytes to copy and must be evenly divisible by the array element size.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
CUarray("srcArray", "source array"),
size_t("srcOffset", "offset in bytes of source array"),
size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyHtoA",
"""
Copies memory from Host to Array.
Copies from host memory to a 1D CUDA array. {@code dstArray} and {@code dstOffset} specify the CUDA array handle and starting offset in bytes of the
destination data. {@code pSrc} specifies the base address of the source. {@code ByteCount} specifies the number of bytes to copy.
""",
CUarray("dstArray", "destination array"),
size_t("dstOffset", "offset in bytes of destination array"),
MultiTypeAll..void.const.p("srcHost", "source host pointer"),
AutoSize("srcHost")..size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyAtoH",
"""
Copies memory from Array to Host.
Copies from one 1D CUDA array to host memory. {@code dstHost} specifies the base pointer of the destination. {@code srcArray} and {@code srcOffset}
specify the CUDA array handle and starting offset in bytes of the source data. {@code ByteCount} specifies the number of bytes to copy.
""",
MultiTypeAll..void.p("dstHost", "destination device pointer"),
CUarray("srcArray", "source array"),
size_t("srcOffset", "offset in bytes of source array"),
AutoSize("dstHost")..size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"MemcpyAtoA",
"""
Copies memory from Array to Array.
Copies from one 1D CUDA array to another. {@code dstArray} and {@code srcArray} specify the handles of the destination and source CUDA arrays for the
copy, respectively. {@code dstOffset} and {@code srcOffset} specify the destination and source offsets in bytes into the CUDA arrays. {@code ByteCount}
is the number of bytes to be copied. The size of the elements in the CUDA arrays need not be the same format, but the elements must be the same size;
and count must be evenly divisible by that size.
""",
CUarray("dstArray", "destination array"),
size_t("dstOffset", "offset in bytes of destination array"),
CUarray("srcArray", "source array"),
size_t("srcOffset", "offset in bytes of source array"),
size_t("ByteCount", "size of memory copy in bytes")
).ptds(2)
CUresult(
"Memcpy2D",
"""
Copies memory for 2D arrays.
Perform a 2D memory copy according to the parameters specified in {@code pCopy}.
If {@code srcMemoryType} is #MEMORYTYPE_UNIFIED, {@code srcDevice} and {@code srcPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code srcArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code srcMemoryType} is #MEMORYTYPE_HOST, {@code srcHost} and {@code srcPitch} specify the (host) base address of the source data and the bytes per
row to apply. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_DEVICE, {@code srcDevice} and {@code srcPitch} specify the (device) base address of the source data and the
bytes per row to apply. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_ARRAY, {@code srcArray} specifies the handle of the source data. {@code srcHost}, {@code srcDevice} and
{@code srcPitch} are ignored.
If {@code dstMemoryType} is #MEMORYTYPE_HOST, {@code dstHost} and {@code dstPitch} specify the (host) base address of the destination data and the
bytes per row to apply. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_UNIFIED, {@code dstDevice} and {@code dstPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code dstArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code dstMemoryType} is #MEMORYTYPE_DEVICE, {@code dstDevice} and {@code dstPitch} specify the (device) base address of the destination data and
the bytes per row to apply. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_ARRAY, {@code dstArray} specifies the handle of the destination data. {@code dstHost}, {@code dstDevice} and
{@code dstPitch} are ignored.
{@code srcXInBytes} and {@code srcY} specify the base address of the source data for the copy.
For host pointers, the starting address is
${codeBlock("""
voidStart = (void*)((char*)srcHost+srcY*srcPitch + srcXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr Start = srcDevice+srcY*srcPitch+srcXInBytes;""")}
For CUDA arrays, {@code srcXInBytes} must be evenly divisible by the array element size.
{@code dstXInBytes} and {@code dstY} specify the base address of the destination data for the copy.
For host pointers, the base address is
${codeBlock("""
voiddstStart = (void*)((char*)dstHost+dstY*dstPitch + dstXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr dstStart = dstDevice+dstY*dstPitch+dstXInBytes;""")}
For CUDA arrays, {@code dstXInBytes} must be evenly divisible by the array element size.
{@code WidthInBytes} and {@code Height} specify the width (in bytes) and height of the 2D copy being performed.
If specified, {@code srcPitch} must be greater than or equal to {@code WidthInBytes} + {@code srcXInBytes}, and {@code dstPitch} must be greater than
or equal to {@code WidthInBytes} + {@code dstXInBytes}.
{@code cuMemcpy2D()} returns an error if any pitch is greater than the maximum allowed (#DEVICE_ATTRIBUTE_MAX_PITCH). {@code cuMemAllocPitch}() passes back
pitches that always work with {@code cuMemcpy2D()}. On intra-device memory copies (device to device, CUDA array to device, CUDA array to CUDA array),
{@code cuMemcpy2D()} may fail for pitches not computed by #MemAllocPitch(). #Memcpy2DUnaligned() does not have this restriction, but may run
significantly slower in the cases where {@code cuMemcpy2D()} would have returned an error code.
""",
CUDA_MEMCPY2D.const.p("pCopy", "parameters for the memory copy")
).ptds(2)
CUresult(
"Memcpy2DUnaligned",
"""
Copies memory for 2D arrays.
Perform a 2D memory copy according to the parameters specified in {@code pCopy}.
If {@code srcMemoryType} is #MEMORYTYPE_UNIFIED, {@code srcDevice} and {@code srcPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code srcArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code srcMemoryType} is #MEMORYTYPE_HOST, {@code srcHost} and {@code srcPitch} specify the (host) base address of the source data and the bytes per
row to apply. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_DEVICE, {@code srcDevice} and {@code srcPitch} specify the (device) base address of the source data and the
bytes per row to apply. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_ARRAY, {@code srcArray} specifies the handle of the source data. {@code srcHost}, {@code srcDevice} and
{@code srcPitch} are ignored.
If {@code dstMemoryType} is #MEMORYTYPE_UNIFIED, {@code dstDevice} and {@code dstPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code dstArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code dstMemoryType} is #MEMORYTYPE_HOST, {@code dstHost} and {@code dstPitch} specify the (host) base address of the destination data and the
bytes per row to apply. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_DEVICE, {@code dstDevice} and {@code dstPitch} specify the (device) base address of the destination data and
the bytes per row to apply. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_ARRAY, {@code dstArray} specifies the handle of the destination data. {@code dstHost}, {@code dstDevice} and
{@code dstPitch} are ignored.
{@code srcXInBytes} and {@code srcY} specify the base address of the source data for the copy.
For host pointers, the starting address is
${codeBlock("""
void* Start = (void*)((char*)srcHost+srcY*srcPitch + srcXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr Start = srcDevice+srcY*srcPitch+srcXInBytes;""")}
For CUDA arrays, {@code srcXInBytes} must be evenly divisible by the array element size.
{@code dstXInBytes} and {@code dstY} specify the base address of the destination data for the copy.
For host pointers, the base address is
${codeBlock("""
void* dstStart = (void*)((char*)dstHost+dstY*dstPitch + dstXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr dstStart = dstDevice+dstY*dstPitch+dstXInBytes;""")}
For CUDA arrays, {@code dstXInBytes} must be evenly divisible by the array element size.
{@code WidthInBytes} and {@code Height} specify the width (in bytes) and height of the 2D copy being performed.
If specified, {@code srcPitch} must be greater than or equal to {@code WidthInBytes} + {@code srcXInBytes}, and {@code dstPitch} must be greater than
or equal to {@code WidthInBytes} + {@code dstXInBytes}.
#Memcpy2D() returns an error if any pitch is greater than the maximum allowed (#DEVICE_ATTRIBUTE_MAX_PITCH). #MemAllocPitch() passes back pitches that
always work with {@code cuMemcpy2D()}. On intra-device memory copies (device to device, CUDA array to device, CUDA array to CUDA array),
{@code cuMemcpy2D()} may fail for pitches not computed by {@code cuMemAllocPitch()}. {@code cuMemcpy2DUnaligned()} does not have this restriction, but
may run significantly slower in the cases where {@code cuMemcpy2D()} would have returned an error code.
""",
CUDA_MEMCPY2D.const.p("pCopy", "parameters for the memory copy")
).ptds(2)
CUresult(
"Memcpy3D",
"""
Copies memory for 3D arrays.
Perform a 3D memory copy according to the parameters specified in {@code pCopy}.
If {@code srcMemoryType} is #MEMORYTYPE_UNIFIED, {@code srcDevice} and {@code srcPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code srcArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code srcMemoryType} is #MEMORYTYPE_HOST, {@code srcHost}, {@code srcPitch} and {@code srcHeight} specify the (host) base address of the source
data, the bytes per row, and the height of each 2D slice of the 3D array. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_DEVICE, {@code srcDevice}, {@code srcPitch} and {@code srcHeight} specify the (device) base address of the
source data, the bytes per row, and the height of each 2D slice of the 3D array. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_ARRAY, {@code srcArray} specifies the handle of the source data. {@code srcHost}, {@code srcDevice},
{@code srcPitch} and {@code srcHeight} are ignored.
If {@code dstMemoryType} is #MEMORYTYPE_UNIFIED, {@code dstDevice} and {@code dstPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code dstArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code dstMemoryType} is #MEMORYTYPE_HOST, {@code dstHost} and {@code dstPitch} specify the (host) base address of the destination data, the bytes
per row, and the height of each 2D slice of the 3D array. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_DEVICE, {@code dstDevice} and {@code dstPitch} specify the (device) base address of the destination data, the
bytes per row, and the height of each 2D slice of the 3D array. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_ARRAY, {@code dstArray} specifies the handle of the destination data. {@code dstHost}, {@code dstDevice},
{@code dstPitch} and {@code dstHeight} are ignored.
{@code srcXInBytes}, {@code srcY} and {@code srcZ} specify the base address of the source data for the copy.
For host pointers, the starting address is
${codeBlock("""
void* Start = (void*)((char*)srcHost+(srcZ*srcHeight+srcY)*srcPitch + srcXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr Start = srcDevice+(srcZ*srcHeight+srcY)*srcPitch+srcXInBytes;""")}
For CUDA arrays, {@code srcXInBytes} must be evenly divisible by the array element size.
{@code dstXInBytes}, {@code dstY} and {@code dstZ} specify the base address of the destination data for the copy.
For host pointers, the base address is
${codeBlock("""
void* dstStart = (void*)((char*)dstHost+(dstZ*dstHeight+dstY)*dstPitch + dstXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr dstStart = dstDevice+(dstZ*dstHeight+dstY)*dstPitch+dstXInBytes;""")}
For CUDA arrays, {@code dstXInBytes} must be evenly divisible by the array element size.
{@code WidthInBytes}, {@code Height} and {@code Depth} specify the width (in bytes), height and depth of the 3D copy being performed.
If specified, {@code srcPitch} must be greater than or equal to {@code WidthInBytes} + {@code srcXInBytes}, and {@code dstPitch} must be greater than
or equal to {@code WidthInBytes} + {@code dstXInBytes}.
If specified, {@code srcHeight} must be greater than or equal to {@code Height} + {@code srcY}, and {@code dstHeight} must be greater than or equal to
{@code Height} + {@code dstY}.
#Memcpy3D() returns an error if any pitch is greater than the maximum allowed (#DEVICE_ATTRIBUTE_MAX_PITCH).
The {@code srcLOD} and {@code dstLOD} members of the {@code CUDA_MEMCPY3D} structure must be set to 0.
${note("""_sync""")}
""",
CUDA_MEMCPY3D.const.p("pCopy", "parameters for the memory copy")
).ptds(2)
IgnoreMissing..CUresult(
"Memcpy3DPeer",
"""
Copies memory between contexts.
Perform a 3D memory copy according to the parameters specified in {@code pCopy}.
""",
CUDA_MEMCPY3D_PEER.const.p("pCopy", "parameters for the memory copy")
).ptds()
IgnoreMissing..CUresult(
"MemcpyAsync",
"""
Copies memory asynchronously.
Copies data between two pointers. {@code dst} and {@code src} are base pointers of the destination and source, respectively. {@code ByteCount}
specifies the number of bytes to copy. Note that this function infers the type of the transfer (host to host, host to device, device to device, or
device to host) from the pointer values. This function is only allowed in contexts which support unified addressing.
""",
CUdeviceptr("dst", "destination unified virtual address space pointer"),
CUdeviceptr("src", "source unified virtual address space pointer"),
size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
IgnoreMissing..CUresult(
"MemcpyPeerAsync",
"""
Copies device memory between two contexts asynchronously.
Copies from device memory in one context to device memory in another context. {@code dstDevice} is the base device pointer of the destination memory
and {@code dstContext} is the destination context. {@code srcDevice} is the base device pointer of the source memory and {@code srcContext} is the
source pointer. {@code ByteCount} specifies the number of bytes to copy.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
CUcontext("dstContext", "destination context"),
CUdeviceptr("srcDevice", "source device pointer"),
CUcontext("srcContext", "source context"),
size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemcpyHtoDAsync",
"""
Copies memory from Host to Device.
Copies from host memory to device memory. {@code dstDevice} and {@code srcHost} are the base addresses of the destination and source, respectively.
{@code ByteCount} specifies the number of bytes to copy.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
MultiTypeAll..void.const.p("srcHost", "source host pointer"),
AutoSize("srcHost")..size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
CUresult(
"MemcpyDtoHAsync",
"""
Copies memory from Device to Host.
Copies from device to host memory. {@code dstHost} and {@code srcDevice} specify the base pointers of the destination and source, respectively. {@code
ByteCount} specifies the number of bytes to copy.
""",
MultiTypeAll..void.p("dstHost", "destination host pointer"),
CUdeviceptr("srcDevice", "source device pointer"),
AutoSize("dstHost")..size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
CUresult(
"MemcpyDtoDAsync",
"""
Copies memory from Device to Device.
Copies from device memory to device memory. {@code dstDevice} and {@code srcDevice} are the base pointers of the destination and source, respectively.
{@code ByteCount} specifies the number of bytes to copy.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
CUdeviceptr("srcDevice", "source device pointer"),
size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
CUresult(
"MemcpyHtoAAsync",
"""
Copies memory from Host to Array.
Copies from host memory to a 1D CUDA array. {@code dstArray} and {@code dstOffset} specify the CUDA array handle and starting offset in bytes of the
destination data. {@code srcHost} specifies the base address of the source. {@code ByteCount} specifies the number of bytes to copy.
""",
CUarray("dstArray", "destination array"),
size_t("dstOffset", "offset in bytes of destination array"),
MultiTypeAll..void.const.p("srcHost", "source host pointer"),
AutoSize("srcHost")..size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
CUresult(
"MemcpyAtoHAsync",
"""
Copies memory from Array to Host.
Copies from one 1D CUDA array to host memory. {@code dstHost} specifies the base pointer of the destination. {@code srcArray} and {@code srcOffset}
specify the CUDA array handle and starting offset in bytes of the source data. {@code ByteCount} specifies the number of bytes to copy.
""",
MultiTypeAll..void.p("dstHost", "destination pointer"),
CUarray("srcArray", "source array"),
size_t("srcOffset", "offset in bytes of source array"),
AutoSize("dstHost")..size_t("ByteCount", "size of memory copy in bytes"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
CUresult(
"Memcpy2DAsync",
"""
Copies memory for 2D arrays.
Perform a 2D memory copy according to the parameters specified in {@code pCopy}.
If {@code srcMemoryType} is #MEMORYTYPE_HOST, {@code srcHost} and {@code srcPitch} specify the (host) base address of the source data and the bytes per
row to apply. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_UNIFIED, {@code srcDevice} and {@code srcPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code srcArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code srcMemoryType} is #MEMORYTYPE_DEVICE, {@code srcDevice} and {@code srcPitch} specify the (device) base address of the source data and the
bytes per row to apply. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_ARRAY, {@code srcArray} specifies the handle of the source data. {@code srcHost}, {@code srcDevice} and
{@code srcPitch} are ignored.
If {@code dstMemoryType} is #MEMORYTYPE_UNIFIED, {@code dstDevice} and {@code dstPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code dstArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code dstMemoryType} is #MEMORYTYPE_HOST, {@code dstHost} and {@code dstPitch} specify the (host) base address of the destination data and the
bytes per row to apply. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_DEVICE, {@code dstDevice} and {@code dstPitch} specify the (device) base address of the destination data and
the bytes per row to apply. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_ARRAY, {@code dstArray} specifies the handle of the destination data. {@code dstHost}, {@code dstDevice} and
{@code dstPitch} are ignored.
{@code srcXInBytes} and {@code srcY} specify the base address of the source data for the copy.
For host pointers, the starting address is
${codeBlock("""
void* Start = (void*)((char*)srcHost+srcY*srcPitch + srcXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr Start = srcDevice+srcY*srcPitch+srcXInBytes;""")}
For CUDA arrays, {@code srcXInBytes} must be evenly divisible by the array element size.
{@code dstXInBytes} and {@code dstY} specify the base address of the destination data for the copy.
For host pointers, the base address is
${codeBlock("""
void* dstStart = (void*)((char*)dstHost+dstY*dstPitch + dstXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr dstStart = dstDevice+dstY*dstPitch+dstXInBytes;""")}
For CUDA arrays, {@code dstXInBytes} must be evenly divisible by the array element size.
{@code WidthInBytes} and {@code Height} specify the width (in bytes) and height of the 2D copy being performed.
If specified, {@code srcPitch} must be greater than or equal to {@code WidthInBytes} + {@code srcXInBytes}, and {@code dstPitch} must be greater than
or equal to {@code WidthInBytes} + {@code dstXInBytes}.
If specified, {@code srcPitch} must be greater than or equal to {@code WidthInBytes} + {@code srcXInBytes}, and {@code dstPitch} must be greater than
or equal to {@code WidthInBytes} + {@code dstXInBytes}.
If specified, {@code srcHeight} must be greater than or equal to {@code Height} + {@code srcY}, and {@code dstHeight} must be greater than or equal to
{@code Height} + {@code dstY}.
{@code cuMemcpy2DAsync()} returns an error if any pitch is greater than the maximum allowed (#DEVICE_ATTRIBUTE_MAX_PITCH). #MemAllocPitch() passes back
pitches that always work with #Memcpy2D(). On intra-device memory copies (device to device, CUDA array to device, CUDA array to CUDA array),
{@code cuMemcpy2DAsync()} may fail for pitches not computed by {@code cuMemAllocPitch()}.
""",
CUDA_MEMCPY2D.const.p("pCopy", "parameters for the memory copy"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
CUresult(
"Memcpy3DAsync",
"""
Copies memory for 3D arrays.
Perform a 3D memory copy according to the parameters specified in {@code pCopy}.
If {@code srcMemoryType} is #MEMORYTYPE_UNIFIED, {@code srcDevice} and {@code srcPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code srcArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code srcMemoryType} is #MEMORYTYPE_HOST, {@code srcHost}, {@code srcPitch} and {@code srcHeight} specify the (host) base address of the source
data, the bytes per row, and the height of each 2D slice of the 3D array. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_DEVICE, {@code srcDevice}, {@code srcPitch} and {@code srcHeight} specify the (device) base address of the
source data, the bytes per row, and the height of each 2D slice of the 3D array. {@code srcArray} is ignored.
If {@code srcMemoryType} is #MEMORYTYPE_ARRAY, {@code srcArray} specifies the handle of the source data. {@code srcHost}, {@code srcDevice},
{@code srcPitch} and {@code srcHeight} are ignored.
If {@code dstMemoryType} is #MEMORYTYPE_UNIFIED, {@code dstDevice} and {@code dstPitch} specify the (unified virtual address space) base address of the
source data and the bytes per row to apply. {@code dstArray} is ignored. This value may be used only if unified addressing is supported in the calling
context.
If {@code dstMemoryType} is #MEMORYTYPE_HOST, {@code dstHost} and {@code dstPitch} specify the (host) base address of the destination data, the bytes
per row, and the height of each 2D slice of the 3D array. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_DEVICE, {@code dstDevice} and {@code dstPitch} specify the (device) base address of the destination data, the
bytes per row, and the height of each 2D slice of the 3D array. {@code dstArray} is ignored.
If {@code dstMemoryType} is #MEMORYTYPE_ARRAY, {@code dstArray} specifies the handle of the destination data. {@code dstHost}, {@code dstDevice},
{@code dstPitch} and {@code dstHeight} are ignored.
- {@code srcXInBytes}, {@code srcY} and {@code srcZ} specify the base address of the source data for the copy.
For host pointers, the starting address is
${codeBlock("""
void* Start = (void*)((char*)srcHost+(srcZ*srcHeight+srcY)*srcPitch + srcXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr Start = srcDevice+(srcZ*srcHeight+srcY)*srcPitch+srcXInBytes;""")}
For CUDA arrays, {@code srcXInBytes} must be evenly divisible by the array element size.
{@code dstXInBytes}, {@code dstY} and {@code dstZ} specify the base address of the destination data for the copy.
For host pointers, the base address is
${codeBlock("""
void* dstStart = (void*)((char*)dstHost+(dstZ*dstHeight+dstY)*dstPitch + dstXInBytes);""")}
For device pointers, the starting address is
${codeBlock("""
CUdeviceptr dstStart = dstDevice+(dstZ*dstHeight+dstY)*dstPitch+dstXInBytes;""")}
For CUDA arrays, {@code dstXInBytes} must be evenly divisible by the array element size.
{@code WidthInBytes}, {@code Height} and {@code Depth} specify the width (in bytes), height and depth of the 3D copy being performed.
If specified, {@code srcPitch} must be greater than or equal to {@code WidthInBytes} + {@code srcXInBytes}, and {@code dstPitch} must be greater than
or equal to {@code WidthInBytes} + {@code dstXInBytes}.
If specified, {@code srcHeight} must be greater than or equal to {@code Height} + {@code srcY}, and {@code dstHeight} must be greater than or equal to
{@code Height} + {@code dstY}.
#Memcpy3DAsync() returns an error if any pitch is greater than the maximum allowed (#DEVICE_ATTRIBUTE_MAX_PITCH).
The {@code srcLOD} and {@code dstLOD} members of the {@code CUDA_MEMCPY3D} structure must be set to 0.
""",
CUDA_MEMCPY3D.const.p("pCopy", "parameters for the memory copy"),
nullable..CUstream("hStream", "stream identifier")
).ptsz(2)
IgnoreMissing..CUresult(
"Memcpy3DPeerAsync",
"""
Copies memory between contexts asynchronously.
Perform a 3D memory copy according to the parameters specified in {@code pCopy}.
""",
CUDA_MEMCPY3D_PEER.const.p("pCopy", "parameters for the memory copy"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemsetD8",
"""
Initializes device memory.
Sets the memory range of {@code N} 8-bit values to the specified value {@code uc}.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
unsigned_char("uc", "value to set"),
size_t("N", "number of elements")
).ptds(2)
CUresult(
"MemsetD16",
"""
Initializes device memory.
Sets the memory range of {@code N} 16-bit values to the specified value {@code us}. The {@code dstDevice} pointer must be two byte aligned.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
unsigned_short("us", "value to set"),
size_t("N", "number of elements")
).ptds(2)
CUresult(
"MemsetD32",
"""
Initializes device memory
Sets the memory range of {@code N} 32-bit values to the specified value {@code ui}. The {@code dstDevice} pointer must be four byte aligned.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
unsigned_int("ui", "value to set"),
size_t("N", "number of elements")
).ptds(2)
CUresult(
"MemsetD2D8",
"""
Initializes device memory.
Sets the 2D memory range of {@code Width} 8-bit values to the specified value {@code uc}. {@code Height} specifies the number of rows to set, and
{@code dstPitch} specifies the number of bytes between each row. This function performs fastest when the pitch is one that has been passed back by
#MemAllocPitch().
""",
CUdeviceptr("dstDevice", "destination device pointer"),
size_t("dstPitch", "pitch of destination device pointer(Unused if {@code Height} is 1)"),
unsigned_char("uc", "value to set"),
size_t("Width", "width of row"),
size_t("Height", "number of rows")
).ptds(2)
CUresult(
"MemsetD2D16",
"""
Initializes device memory.
Sets the 2D memory range of {@code Width} 16-bit values to the specified value {@code us}. {@code Height} specifies the number of rows to set, and
{@code dstPitch} specifies the number of bytes between each row. The {@code dstDevice} pointer and {@code dstPitch} offset must be two byte aligned.
This function performs fastest when the pitch is one that has been passed back by #MemAllocPitch().
""",
CUdeviceptr("dstDevice", "destination device pointer"),
size_t("dstPitch", "pitch of destination device pointer(Unused if {@code Height} is 1)"),
unsigned_short("us", "value to set"),
size_t("Width", "width of row"),
size_t("Height", "number of rows")
).ptds(2)
CUresult(
"MemsetD2D32",
"""
Initializes device memory.
Sets the 2D memory range of {@code Width} 32-bit values to the specified value {@code ui}. {@code Height} specifies the number of rows to set, and
{@code dstPitch} specifies the number of bytes between each row. The {@code dstDevice} pointer and {@code dstPitch} offset must be four byte aligned.
This function performs fastest when the pitch is one that has been passed back by #MemAllocPitch().
""",
CUdeviceptr("dstDevice", "destination device pointer"),
size_t("dstPitch", "pitch of destination device pointer(Unused if {@code Height} is 1)"),
unsigned_int("ui", "value to set"),
size_t("Width", "width of row"),
size_t("Height", "number of rows")
).ptds(2)
CUresult(
"MemsetD8Async",
"""
Sets device memory
Sets the memory range of {@code N} 8-bit values to the specified value {@code uc}.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
unsigned_char("uc", "value to set"),
size_t("N", "number of elements"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemsetD16Async",
"""
Sets device memory
Sets the memory range of {@code N} 16-bit values to the specified value {@code us}. The {@code dstDevice} pointer must be two byte aligned.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
unsigned_short("us", "value to set"),
size_t("N", "number of elements"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemsetD32Async",
"""
Sets device memory.
Sets the memory range of {@code N} 32-bit values to the specified value {@code ui}. The {@code dstDevice} pointer must be four byte aligned.
""",
CUdeviceptr("dstDevice", "destination device pointer"),
unsigned_int("ui", "value to set"),
size_t("N", "number of elements"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemsetD2D8Async",
"""
Sets device memory.
Sets the 2D memory range of {@code Width} 8-bit values to the specified value {@code uc}. {@code Height} specifies the number of rows to set, and
{@code dstPitch} specifies the number of bytes between each row. This function performs fastest when the pitch is one that has been passed back by
#MemAllocPitch().
""",
CUdeviceptr("dstDevice", "destination device pointer"),
size_t("dstPitch", "pitch of destination device pointer(Unused if {@code Height} is 1)"),
unsigned_char("uc", "value to set"),
size_t("Width", "width of row"),
size_t("Height", "number of rows"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemsetD2D16Async",
"""
Sets device memory.
Sets the 2D memory range of {@code Width} 16-bit values to the specified value {@code us}. {@code Height} specifies the number of rows to set, and
{@code dstPitch} specifies the number of bytes between each row. The {@code dstDevice} pointer and {@code dstPitch} offset must be two byte aligned.
This function performs fastest when the pitch is one that has been passed back by #MemAllocPitch().
""",
CUdeviceptr("dstDevice", "destination device pointer"),
size_t("dstPitch", "pitch of destination device pointer(Unused if {@code Height} is 1)"),
unsigned_short("us", "value to set"),
size_t("Width", "width of row"),
size_t("Height", "number of rows"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"MemsetD2D32Async",
"""
Sets device memory.
Sets the 2D memory range of {@code Width} 32-bit values to the specified value {@code ui}. {@code Height} specifies the number of rows to set, and
{@code dstPitch} specifies the number of bytes between each row. The {@code dstDevice} pointer and {@code dstPitch} offset must be four byte aligned.
This function performs fastest when the pitch is one that has been passed back by #MemAllocPitch().
""",
CUdeviceptr("dstDevice", "destination device pointer"),
size_t("dstPitch", "pitch of destination device pointer(Unused if {@code Height} is 1)"),
unsigned_int("ui", "value to set"),
size_t("Width", "width of row"),
size_t("Height", "number of rows"),
nullable..CUstream("hStream", "stream identifier")
).ptsz()
CUresult(
"ArrayCreate",
"""
Creates a 1D or 2D CUDA array.
Creates a CUDA array according to the {@code CUDA_ARRAY_DESCRIPTOR} structure {@code pAllocateArray} and returns a handle to the new CUDA array in
{@code *pHandle}.
""",
Check(1)..CUarray.p("pHandle", "returned array"),
CUDA_ARRAY_DESCRIPTOR.const.p("pAllocateArray", "array descriptor")
).versioned()
CUresult(
"ArrayGetDescriptor",
"""
Get a 1D or 2D CUDA array descriptor.
Returns in {@code *pArrayDescriptor} a descriptor containing information on the format and dimensions of the CUDA array {@code hArray}. It is useful
for subroutines that have been passed a CUDA array, but need to know the CUDA array parameters for validation or other purposes.
""",
CUDA_ARRAY_DESCRIPTOR.p("pArrayDescriptor", "returned array descriptor"),
CUarray("hArray", "array to get descriptor of")
).versioned()
IgnoreMissing..CUresult(
"ArrayGetSparseProperties",
"""
Returns the layout properties of a sparse CUDA array.
Returns the layout properties of a sparse CUDA array in {@code sparseProperties} If the CUDA array is not allocated with flag #CUDA_ARRAY3D_SPARSE
#CUDA_ERROR_INVALID_VALUE will be returned.
If the returned value in ##CUDA_ARRAY_SPARSE_PROPERTIES{@code flags} contains #ARRAY_SPARSE_PROPERTIES_SINGLE_MIPTAIL, then
{@code CUDA_ARRAY_SPARSE_PROPERTIES::miptailSize} represents the total size of the array. Otherwise, it will be zero. Also, the returned value in
{@code CUDA_ARRAY_SPARSE_PROPERTIES::miptailFirstLevel} is always zero. Note that the {@code array} must have been allocated using #ArrayCreate() or
#Array3DCreate(). For CUDA arrays obtained using #MipmappedArrayGetLevel(), #CUDA_ERROR_INVALID_VALUE will be returned. Instead,
#MipmappedArrayGetSparseProperties() must be used to obtain the sparse properties of the entire CUDA mipmapped array to which {@code array} belongs to.
""",
CUDA_ARRAY_SPARSE_PROPERTIES.p("sparseProperties", "pointer to {@code CUDA_ARRAY_SPARSE_PROPERTIES}"),
CUarray("array", "CUDA array to get the sparse properties of")
)
IgnoreMissing..CUresult(
"MipmappedArrayGetSparseProperties",
"""
Returns the layout properties of a sparse CUDA mipmapped array.
Returns the sparse array layout properties in {@code sparseProperties} If the CUDA mipmapped array is not allocated with flag #CUDA_ARRAY3D_SPARSE
#CUDA_ERROR_INVALID_VALUE will be returned.
For non-layered CUDA mipmapped arrays, ##CUDA_ARRAY_SPARSE_PROPERTIES{@code ::miptailSize} returns the size of the mip tail region. The mip tail region
includes all mip levels whose width, height or depth is less than that of the tile. For layered CUDA mipmapped arrays, if
{@code CUDA_ARRAY_SPARSE_PROPERTIES::flags} contains #ARRAY_SPARSE_PROPERTIES_SINGLE_MIPTAIL, then {@code CUDA_ARRAY_SPARSE_PROPERTIES::miptailSize}
specifies the size of the mip tail of all layers combined. Otherwise, {@code CUDA_ARRAY_SPARSE_PROPERTIES::miptailSize} specifies mip tail size per
layer. The returned value of {@code CUDA_ARRAY_SPARSE_PROPERTIES::miptailFirstLevel} is valid only if {@code CUDA_ARRAY_SPARSE_PROPERTIES::miptailSize}
is non-zero.
""",
CUDA_ARRAY_SPARSE_PROPERTIES.p("sparseProperties", "pointer to {@code CUDA_ARRAY_SPARSE_PROPERTIES}"),
CUmipmappedArray("mipmap", "CUDA mipmapped array to get the sparse properties of")
)
IgnoreMissing..CUresult(
"ArrayGetPlane",
"""
Gets a CUDA array plane from a CUDA array.
Returns in {@code pPlaneArray} a CUDA array that represents a single format plane of the CUDA array {@code hArray}.
If {@code planeIdx} is greater than the maximum number of planes in this array or if the array does not have a multi-planar format e.g:
#AD_FORMAT_NV12, then #CUDA_ERROR_INVALID_VALUE is returned.
Note that if the {@code hArray} has format #AD_FORMAT_NV12, then passing in 0 for {@code planeIdx} returns a CUDA array of the same size as {@code
hArray} but with one channel and #AD_FORMAT_UNSIGNED_INT8 as its format. If 1 is passed for {@code planeIdx}, then the returned CUDA array has half
the height and width of {@code hArray} with two channels and #AD_FORMAT_UNSIGNED_INT8 as its format.
""",
Check(1)..CUarray.p("pPlaneArray", "returned CUDA array referenced by the {@code planeIdx}"),
CUarray("hArray", "multiplanar CUDA array"),
unsigned_int("planeIdx", "plane index")
)
CUresult(
"ArrayDestroy",
"""
Destroys a CUDA array.
Destroys the CUDA array {@code hArray}.
""",
CUarray("hArray", "array to destroy")
)
CUresult(
"Array3DCreate",
"""
Creates a 3D CUDA array.
Creates a CUDA array according to the ##CUDA_ARRAY3D_DESCRIPTOR structure {@code pAllocateArray} and returns a handle to the new CUDA array in {@code
*pHandle}.
${ul(
"""
{@code Width}, {@code Height}, and {@code Depth} are the width, height, and depth of the CUDA array (in elements); the following types of CUDA
arrays can be allocated:
${ul(
"A 1D array is allocated if {@code Height} and {@code Depth} extents are both zero.",
"A 2D array is allocated if only {@code Depth} extent is zero.",
"A 3D array is allocated if all three extents are non-zero.",
"""
A 1D layered CUDA array is allocated if only {@code Height} is zero and the #CUDA_ARRAY3D_LAYERED flag is set. Each layer is a 1D array. The
number of layers is determined by the depth extent.
""",
"""
A 2D layered CUDA array is allocated if all three extents are non-zero and the #CUDA_ARRAY3D_LAYERED flag is set. Each layer is a 2D array. The
number of layers is determined by the depth extent.
""",
"""
A cubemap CUDA array is allocated if all three extents are non-zero and the #CUDA_ARRAY3D_CUBEMAP flag is set. {@code Width} must be equal to
{@code Height}, and {@code Depth} must be six. A cubemap is a special type of 2D layered CUDA array, where the six layers represent the six
faces of a cube. The order of the six layers in memory is the same as that listed in {@code CUarray_cubemap_face}.
""",
"""
A cubemap layered CUDA array is allocated if all three extents are non-zero, and both, #CUDA_ARRAY3D_CUBEMAP and #CUDA_ARRAY3D_LAYERED flags
are set. {@code Width} must be equal to {@code Height}, and {@code Depth} must be a multiple of six. A cubemap layered CUDA array is a special
type of 2D layered CUDA array that consists of a collection of cubemaps. The first six layers represent the first cubemap, the next six layers
form the second cubemap, and so on.
"""
)}
""",
"{@code Format} specifies the format of the elements.",
"{@code NumChannels} specifies the number of packed components per CUDA array element; it may be 1, 2, or 4;",
"""
{@code Flags} may be set to
${ul(
"""
#CUDA_ARRAY3D_LAYERED to enable creation of layered CUDA arrays. If this flag is set, {@code Depth} specifies the number of layers, not the
depth of a 3D array.
""",
"""
#CUDA_ARRAY3D_SURFACE_LDST to enable surface references to be bound to the CUDA array. If this flag is not set, #SurfRefSetArray() will fail
when attempting to bind the CUDA array to a surface reference.
""",
"""
#CUDA_ARRAY3D_CUBEMAP to enable creation of cubemaps. If this flag is set, {@code Width} must be equal to {@code Height}, and {@code Depth}
must be six. If the #CUDA_ARRAY3D_LAYERED flag is also set, then {@code Depth} must be a multiple of six.
""",
"""
#CUDA_ARRAY3D_TEXTURE_GATHER to indicate that the CUDA array will be used for texture gather. Texture gather can only be performed on 2D CUDA
arrays.
"""
)}
"""
)}
{@code Width}, {@code Height} and {@code Depth} must meet certain size requirements as listed in the following table. All values are specified in
elements. Note that for brevity's sake, the full name of the device attribute is not specified. For ex., TEXTURE1D_WIDTH refers to the device attribute
#DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_WIDTH.
Note that 2D CUDA arrays have different size requirements if the #CUDA_ARRAY3D_TEXTURE_GATHER flag is set. {@code Width} and {@code Height} must not
be greater than #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_GATHER_WIDTH and #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_GATHER_HEIGHT respectively, in that
case.
<table>
<tr><td><b>CUDA array type</b></td>
<td><b>Valid extents that must always be met<br>{(width range in elements), (height range),
(depth range)}</b></td>
<td><b>Valid extents with CUDA_ARRAY3D_SURFACE_LDST set<br>
{(width range in elements), (height range), (depth range)}</b></td></tr>
<tr><td>1D</td>
<td><small>{ (1,TEXTURE1D_WIDTH), 0, 0 }</small></td>
<td><small>{ (1,SURFACE1D_WIDTH), 0, 0 }</small></td></tr>
<tr><td>2D</td>
<td><small>{ (1,TEXTURE2D_WIDTH), (1,TEXTURE2D_HEIGHT), 0 }</small></td>
<td><small>{ (1,SURFACE2D_WIDTH), (1,SURFACE2D_HEIGHT), 0 }</small></td></tr>
<tr><td>3D</td>
<td><small>{ (1,TEXTURE3D_WIDTH), (1,TEXTURE3D_HEIGHT), (1,TEXTURE3D_DEPTH) }
<br>OR<br>{ (1,TEXTURE3D_WIDTH_ALTERNATE), (1,TEXTURE3D_HEIGHT_ALTERNATE),
(1,TEXTURE3D_DEPTH_ALTERNATE) }</small></td>
<td><small>{ (1,SURFACE3D_WIDTH), (1,SURFACE3D_HEIGHT),
(1,SURFACE3D_DEPTH) }</small></td></tr>
<tr><td>1D Layered</td>
<td><small>{ (1,TEXTURE1D_LAYERED_WIDTH), 0,
(1,TEXTURE1D_LAYERED_LAYERS) }</small></td>
<td><small>{ (1,SURFACE1D_LAYERED_WIDTH), 0,
(1,SURFACE1D_LAYERED_LAYERS) }</small></td></tr>
<tr><td>2D Layered</td>
<td><small>{ (1,TEXTURE2D_LAYERED_WIDTH), (1,TEXTURE2D_LAYERED_HEIGHT),
(1,TEXTURE2D_LAYERED_LAYERS) }</small></td>
<td><small>{ (1,SURFACE2D_LAYERED_WIDTH), (1,SURFACE2D_LAYERED_HEIGHT),
(1,SURFACE2D_LAYERED_LAYERS) }</small></td></tr>
<tr><td>Cubemap</td>
<td><small>{ (1,TEXTURECUBEMAP_WIDTH), (1,TEXTURECUBEMAP_WIDTH), 6 }</small></td>
<td><small>{ (1,SURFACECUBEMAP_WIDTH),
(1,SURFACECUBEMAP_WIDTH), 6 }</small></td></tr>
<tr><td>Cubemap Layered</td>
<td><small>{ (1,TEXTURECUBEMAP_LAYERED_WIDTH), (1,TEXTURECUBEMAP_LAYERED_WIDTH),
(1,TEXTURECUBEMAP_LAYERED_LAYERS) }</small></td>
<td><small>{ (1,SURFACECUBEMAP_LAYERED_WIDTH), (1,SURFACECUBEMAP_LAYERED_WIDTH),
(1,SURFACECUBEMAP_LAYERED_LAYERS) }</small></td></tr>
</table>
""",
Check(1)..CUarray.p("pHandle", "returned array"),
CUDA_ARRAY3D_DESCRIPTOR.const.p("pAllocateArray", "3D array descriptor")
).versioned()
CUresult(
"Array3DGetDescriptor",
"""
Get a 3D CUDA array descriptor.
Returns in {@code *pArrayDescriptor} a descriptor containing information on the format and dimensions of the CUDA array {@code hArray}. It is useful
for subroutines that have been passed a CUDA array, but need to know the CUDA array parameters for validation or other purposes.
This function may be called on 1D and 2D arrays, in which case the {@code Height} and/or {@code Depth} members of the descriptor struct will be set to
0.
""",
CUDA_ARRAY3D_DESCRIPTOR.p("pArrayDescriptor", "returned 3D array descriptor"),
CUarray("hArray", "3D array to get descriptor of")
).versioned()
IgnoreMissing..CUresult(
"MipmappedArrayCreate",
"""
Creates a CUDA mipmapped array.
Creates a CUDA mipmapped array according to the ##CUDA_ARRAY3D_DESCRIPTOR structure {@code pMipmappedArrayDesc} and returns a handle to the new CUDA
mipmapped array in {@code *pHandle}. {@code numMipmapLevels} specifies the number of mipmap levels to be allocated. This value is clamped to the range
{@code [1, 1 + floor(log2(max(width, height, depth)))]}.
${ul(
"""
{@code Width}, {@code Height}, and {@code Depth} are the width, height, and depth of the CUDA array (in elements); the following types of CUDA
arrays can be allocated:
${ul(
"A 1D mipmapped array is allocated if {@code Height} and {@code Depth} extents are both zero.",
"A 2D mipmapped array is allocated if only {@code Depth} extent is zero.",
"A 3D mipmapped array is allocated if all three extents are non-zero.",
"""
A 1D layered CUDA mipmapped array is allocated if only {@code Height} is zero and the #CUDA_ARRAY3D_LAYERED flag is set. Each layer is a 1D
array. The number of layers is determined by the depth extent.
""",
"""
A 2D layered CUDA mipmapped array is allocated if all three extents are non-zero and the #CUDA_ARRAY3D_LAYERED flag is set. Each layer is a 2D
array. The number of layers is determined by the depth extent.
""",
"""
A cubemap CUDA mipmapped array is allocated if all three extents are non-zero and the #CUDA_ARRAY3D_CUBEMAP flag is set. {@code Width} must be
equal to {@code Height}, and {@code Depth} must be six. A cubemap is a special type of 2D layered CUDA array, where the six layers represent
the six faces of a cube. The order of the six layers in memory is the same as that listed in {@code CUarray_cubemap_face}.
""",
"""
A cubemap layered CUDA mipmapped array is allocated if all three extents are non-zero, and both, #CUDA_ARRAY3D_CUBEMAP and
#CUDA_ARRAY3D_LAYERED flags are set. {@code Width} must be equal to {@code Height}, and {@code Depth} must be a multiple of six. A cubemap
layered CUDA array is a special type of 2D layered CUDA array that consists of a collection of cubemaps. The first six layers represent the
first cubemap, the next six layers form the second cubemap, and so on.
"""
)}
""",
"{@code Format} specifies the format of the elements.",
"{@code NumChannels} specifies the number of packed components per CUDA array element; it may be 1, 2, or 4;",
"""
Flags may be set to:
${ul(
"""
#CUDA_ARRAY3D_LAYERED to enable creation of layered CUDA mipmapped arrays. If this flag is set, {@code Depth} specifies the number of layers,
not the depth of a 3D array.
""",
"""
#CUDA_ARRAY3D_SURFACE_LDST to enable surface references to be bound to individual mipmap levels of the CUDA mipmapped array. If this flag is
not set, #SurfRefSetArray() will fail when attempting to bind a mipmap level of the CUDA mipmapped array to a surface reference.
""",
"""
#CUDA_ARRAY3D_CUBEMAP to enable creation of mipmapped cubemaps. If this flag is set, {@code Width} must be equal to {@code Height}, and
{@code Depth} must be six. If the #CUDA_ARRAY3D_LAYERED flag is also set, then {@code Depth} must be a multiple of six.
""",
"""
#CUDA_ARRAY3D_TEXTURE_GATHER to indicate that the CUDA mipmapped array will be used for texture gather. Texture gather can only be performed on
2D CUDA mipmapped arrays.
"""
)}
"""
)}
{@code Width}, {@code Height} and {@code Depth} must meet certain size requirements as listed in the following table. All values are specified in
elements. Note that for brevity's sake, the full name of the device attribute is not specified. For ex., {@code TEXTURE1D_MIPMAPPED_WIDTH} refers to
the device attribute #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_MIPMAPPED_WIDTH.
<table>
<tr><td><b>CUDA array type</b></td>
<td><b>Valid extents that must always be met<br>{(width range in elements), (height range),
(depth range)}</b></td>
<td><b>Valid extents with CUDA_ARRAY3D_SURFACE_LDST set<br>
{(width range in elements), (height range), (depth range)}</b></td></tr>
<tr><td>1D</td>
<td><small>{ (1,TEXTURE1D_MIPMAPPED_WIDTH), 0, 0 }</small></td>
<td><small>{ (1,SURFACE1D_WIDTH), 0, 0 }</small></td></tr>
<tr><td>2D</td>
<td><small>{ (1,TEXTURE2D_MIPMAPPED_WIDTH), (1,TEXTURE2D_MIPMAPPED_HEIGHT), 0 }</small></td>
<td><small>{ (1,SURFACE2D_WIDTH), (1,SURFACE2D_HEIGHT), 0 }</small></td></tr>
<tr><td>3D</td>
<td><small>{ (1,TEXTURE3D_WIDTH), (1,TEXTURE3D_HEIGHT), (1,TEXTURE3D_DEPTH) }
<br>OR<br>{ (1,TEXTURE3D_WIDTH_ALTERNATE), (1,TEXTURE3D_HEIGHT_ALTERNATE),
(1,TEXTURE3D_DEPTH_ALTERNATE) }</small></td>
<td><small>{ (1,SURFACE3D_WIDTH), (1,SURFACE3D_HEIGHT),
(1,SURFACE3D_DEPTH) }</small></td></tr>
<tr><td>1D Layered</td>
<td><small>{ (1,TEXTURE1D_LAYERED_WIDTH), 0,
(1,TEXTURE1D_LAYERED_LAYERS) }</small></td>
<td><small>{ (1,SURFACE1D_LAYERED_WIDTH), 0,
(1,SURFACE1D_LAYERED_LAYERS) }</small></td></tr>
<tr><td>2D Layered</td>
<td><small>{ (1,TEXTURE2D_LAYERED_WIDTH), (1,TEXTURE2D_LAYERED_HEIGHT),
(1,TEXTURE2D_LAYERED_LAYERS) }</small></td>
<td><small>{ (1,SURFACE2D_LAYERED_WIDTH), (1,SURFACE2D_LAYERED_HEIGHT),
(1,SURFACE2D_LAYERED_LAYERS) }</small></td></tr>
<tr><td>Cubemap</td>
<td><small>{ (1,TEXTURECUBEMAP_WIDTH), (1,TEXTURECUBEMAP_WIDTH), 6 }</small></td>
<td><small>{ (1,SURFACECUBEMAP_WIDTH),
(1,SURFACECUBEMAP_WIDTH), 6 }</small></td></tr>
<tr><td>Cubemap Layered</td>
<td><small>{ (1,TEXTURECUBEMAP_LAYERED_WIDTH), (1,TEXTURECUBEMAP_LAYERED_WIDTH),
(1,TEXTURECUBEMAP_LAYERED_LAYERS) }</small></td>
<td><small>{ (1,SURFACECUBEMAP_LAYERED_WIDTH), (1,SURFACECUBEMAP_LAYERED_WIDTH),
(1,SURFACECUBEMAP_LAYERED_LAYERS) }</small></td></tr>
</table>
""",
Check(1)..CUmipmappedArray.p("pHandle", "returned mipmapped array"),
CUDA_ARRAY3D_DESCRIPTOR.const.p("pMipmappedArrayDesc", "mipmapped array descriptor"),
unsigned_int("numMipmapLevels", "number of mipmap levels")
)
IgnoreMissing..CUresult(
"MipmappedArrayGetLevel",
"""
Gets a mipmap level of a CUDA mipmapped array.
Returns in {@code *pLevelArray} a CUDA array that represents a single mipmap level of the CUDA mipmapped array {@code hMipmappedArray}.
If {@code level} is greater than the maximum number of levels in this mipmapped array, #CUDA_ERROR_INVALID_VALUE is returned.
""",
Check(1)..CUarray.p("pLevelArray", "returned mipmap level CUDA array"),
CUmipmappedArray("hMipmappedArray", "CUDA mipmapped array"),
unsigned_int("level", "mipmap level")
)
IgnoreMissing..CUresult(
"MipmappedArrayDestroy",
"""
Destroys a CUDA mipmapped array.
Destroys the CUDA mipmapped array {@code hMipmappedArray}.
""",
CUmipmappedArray("hMipmappedArray", "mipmapped array to destroy")
)
IgnoreMissing..CUresult(
"MemAddressReserve",
"""
Allocate an address range reservation.
Reserves a virtual address range based on the given parameters, giving the starting address of the range in {@code ptr}. This API requires a system
that supports UVA. The size and address parameters must be a multiple of the host page size and the alignment must be a power of two or zero for
default alignment.
""",
Check(1)..CUdeviceptr.p("ptr", "resulting pointer to start of virtual address range allocated"),
size_t("size", "size of the reserved virtual address range requested"),
size_t("alignment", "alignment of the reserved virtual address range requested"),
CUdeviceptr("addr", "fixed starting address range requested"),
unsigned_long_long("flags", "currently unused, must be zero")
)
IgnoreMissing..CUresult(
"MemAddressFree",
"""
Free an address range reservation.
Frees a virtual address range reserved by #MemAddressReserve(). The size must match what was given to {@code memAddressReserve} and the ptr given must
match what was returned from {@code memAddressReserve}.
""",
CUdeviceptr("ptr", "starting address of the virtual address range to free"),
size_t("size", "size of the virtual address region to free")
)
IgnoreMissing..CUresult(
"MemCreate",
"""
Create a CUDA memory handle representing a memory allocation of a given size described by the given properties.
This creates a memory allocation on the target device specified through the {@code prop} strcuture. The created allocation will not have any device or
host mappings. The generic memory {@code handle} for the allocation can be mapped to the address space of calling process via #MemMap(). This handle
cannot be transmitted directly to other processes (see #MemExportToShareableHandle()). On Windows, the caller must also pass an
{@code LPSECURITYATTRIBUTE} in {@code prop} to be associated with this handle which limits or allows access to this handle for a recepient process (see
##CUmemAllocationProp{@code ::win32HandleMetaData} for more). The {@code size} of this allocation must be a multiple of the the value given via
#MemGetAllocationGranularity() with the #MEM_ALLOC_GRANULARITY_MINIMUM flag. If ##CUmemAllocationProp{@code ::allocFlags::usage} contains
#MEM_CREATE_USAGE_TILE_POOL flag then the memory allocation is intended only to be used as backing tile pool for sparse CUDA arrays and sparse CUDA
mipmapped arrays. (see #MemMapArrayAsync()).
""",
Check(1)..CUmemGenericAllocationHandle.p("handle", "value of handle returned. All operations on this allocation are to be performed using this handle."),
size_t("size", "size of the allocation requested"),
CUmemAllocationProp.const.p("prop", "properties of the allocation to create"),
unsigned_long_long("flags", "flags for future use, must be zero now")
)
IgnoreMissing..CUresult(
"MemRelease",
"""
Release a memory handle representing a memory allocation which was previously allocated through #MemCreate().
Frees the memory that was allocated on a device through {@code cuMemCreate}.
The memory allocation will be freed when all outstanding mappings to the memory are unmapped and when all outstanding references to the handle
(including it's shareable counterparts) are also released. The generic memory handle can be freed when there are still outstanding mappings made with
this handle. Each time a recepient process imports a shareable handle, it needs to pair it with #MemRelease() for the handle to be freed. If {@code
handle} is not a valid handle the behavior is undefined.
""",
CUmemGenericAllocationHandle("handle", "value of handle which was returned previously by {@code cuMemCreate}")
)
IgnoreMissing..CUresult(
"MemMap",
"""
Maps an allocation handle to a reserved virtual address range.
Maps bytes of memory represented by {@code handle} starting from byte {@code offset} to {@code size} to address range [ {@code addr}, {@code addr} +
{@code size]}. This range must be an address reservation previously reserved with #MemAddressReserve(), and {@code offset} + {@code size} must be less
than the size of the memory allocation. Both {@code ptr}, {@code size}, and {@code offset} must be a multiple of the value given via
#MemGetAllocationGranularity() with the #MEM_ALLOC_GRANULARITY_MINIMUM flag.
Please note calling #MemMap() does not make the address accessible, the caller needs to update accessibility of a contiguous mapped VA range by
calling #MemSetAccess().
Once a recipient process obtains a shareable memory handle from #MemImportFromShareableHandle(), the process must use #MemMap() to map the memory
into its address ranges before setting accessibility with #MemSetAccess().
#MemMap() can only create mappings on VA range reservations that are not currently mapped.
""",
CUdeviceptr("ptr", "address where memory will be mapped"),
size_t("size", "size of the memory mapping"),
size_t("offset", "offset into the memory represented by - {@code handle} from which to start mapping - Note: currently must be zero"),
CUmemGenericAllocationHandle("handle", "handle to a shareable memory"),
unsigned_long_long("flags", "flags for future use, must be zero now")
)
IgnoreMissing..CUresult(
"MemMapArrayAsync",
"""
Maps or unmaps subregions of sparse CUDA arrays and sparse CUDA mipmapped arrays.
Performs map or unmap operations on subregions of sparse CUDA arrays and sparse CUDA mipmapped arrays. Each operation is specified by a
##CUarrayMapInfo entry in the {@code mapInfoList} array of size {@code count}.
where {@code CUarrayMapInfo::resourceType} specifies the type of resource to be operated on. If {@code CUarrayMapInfo::resourceType} is set to
#RESOURCE_TYPE_ARRAY then {@code CUarrayMapInfo::resource::array} must be set to a valid sparse CUDA array handle. The CUDA array must be
either a 2D, 2D layered or 3D CUDA array and must have been allocated using #ArrayCreate() or #Array3DCreate() with the flag #CUDA_ARRAY3D_SPARSE.
For CUDA arrays obtained using #MipmappedArrayGetLevel(), #CUDA_ERROR_INVALID_VALUE will be returned. If {@code CUarrayMapInfo::resourceType} is set to
#RESOURCE_TYPE_MIPMAPPED_ARRAY then {@code CUarrayMapInfo::resource::mipmap} must be set to a valid sparse CUDA mipmapped array handle.
The CUDA mipmapped array must be either a 2D, 2D layered or 3D CUDA mipmapped array and must have been allocated using #MipmappedArrayCreate() with
the flag #CUDA_ARRAY3D_SPARSE.
{@code CUarrayMapInfo::subresourceType} specifies the type of subresource within the resource.
where #ARRAY_SPARSE_SUBRESOURCE_TYPE_SPARSE_LEVEL indicates a sparse-miplevel which spans at least one tile in every dimension. The remaining miplevels
which are too small to span at least one tile in any dimension constitute the mip tail region as indicated by #ARRAY_SPARSE_SUBRESOURCE_TYPE_MIPTAIL
subresource type.
If {@code CUarrayMapInfo::subresourceType} is set to #ARRAY_SPARSE_SUBRESOURCE_TYPE_SPARSE_LEVEL then
{@code CUarrayMapInfo::subresource::sparseLevel} struct must contain valid array subregion offsets and extents. The
{@code CUarrayMapInfo::subresource::sparseLevel::offsetX}, {@code CUarrayMapInfo::subresource::sparseLevel::offsetY} and
{@code CUarrayMapInfo::subresource::sparseLevel::offsetZ} must specify valid X, Y and Z offsets respectively. The
{@code CUarrayMapInfo::subresource::sparseLevel::extentWidth}, {@code CUarrayMapInfo::subresource::sparseLevel::extentHeight} and
{@code CUarrayMapInfo::subresource::sparseLevel::extentDepth} must specify valid width, height and depth extents respectively. These offsets and
extents must be aligned to the corresponding tile dimension. For CUDA mipmapped arrays {@code CUarrayMapInfo::subresource::sparseLevel::level} must
specify a valid mip level index. Otherwise, must be zero. For layered CUDA arrays and layered CUDA mipmapped arrays
{@code CUarrayMapInfo::subresource::sparseLevel::layer} must specify a valid layer index. Otherwise, must be zero.
{@code CUarrayMapInfo::subresource::sparseLevel::offsetZ} must be zero and {@code CUarrayMapInfo::subresource::sparseLevel::extentDepth} must be set to
1 for 2D and 2D layered CUDA arrays and CUDA mipmapped arrays. Tile extents can be obtained by calling #ArrayGetSparseProperties() and
#MipmappedArrayGetSparseProperties()
If {@code CUarrayMapInfo::subresourceType} is set to #ARRAY_SPARSE_SUBRESOURCE_TYPE_MIPTAIL then {@code CUarrayMapInfo::subresource::miptail} struct
must contain valid mip tail offset in {@code CUarrayMapInfo::subresource::miptail::offset} and size in
{@code CUarrayMapInfo::subresource::miptail::size}. Both, mip tail offset and mip tail size must be aligned to the tile size. For layered CUDA
mipmapped arrays which don't have the flag #ARRAY_SPARSE_PROPERTIES_SINGLE_MIPTAIL set in ##CUDA_ARRAY_SPARSE_PROPERTIES{@code ::flags} as returned by
#MipmappedArrayGetSparseProperties(), {@code CUarrayMapInfo::subresource::miptail::layer} must specify a valid layer index. Otherwise, must be zero.
{@code CUarrayMapInfo::memOperationType} specifies the type of operation.
If {@code CUarrayMapInfo::memOperationType} is set to #MEM_OPERATION_TYPE_MAP then the subresource will be mapped onto the tile pool memory specified
by {@code CUarrayMapInfo::memHandle} at offset {@code CUarrayMapInfo::offset}. The tile pool allocation has to be created by specifying the
#MEM_CREATE_USAGE_TILE_POOL flag when calling #MemCreate(). Also, {@code CUarrayMapInfo::memHandleType} must be set to #MEM_HANDLE_TYPE_GENERIC.
If {@code CUarrayMapInfo::memOperationType} is set to #MEM_OPERATION_TYPE_UNMAP then an unmapping operation is performed.
{@code CUarrayMapInfo::memHandle} must be NULL.
{@code CUarrayMapInfo::deviceBitMask} specifies the list of devices that must map or unmap physical memory. Currently, this mask must have exactly one
bit set, and the corresponding device must match the device associated with the stream. If {@code CUarrayMapInfo::memOperationType} is set to
#MEM_OPERATION_TYPE_MAP, the device must also match the device associated with the tile pool memory allocation as specified by
{@code CUarrayMapInfo::memHandle}.
{@code CUarrayMapInfo::flags} and {@code CUarrayMapInfo::reserved[]} are unused and must be set to zero.
""",
CUarrayMapInfo.p("mapInfoList", "list of {@code CUarrayMapInfo}"),
AutoSize("mapInfoList")..unsigned_int("count", "count of {@code CUarrayMapInfo} in {@code mapInfoList}"),
nullable..CUstream("hStream", "stream identifier for the stream to use for map or unmap operations")
).ptsz()
IgnoreMissing..CUresult(
"MemUnmap",
"""
Unmap the backing memory of a given address range.
The range must be the entire contiguous address range that was mapped to. In other words, #MemUnmap() cannot unmap a sub-range of an address range
mapped by #MemCreate() / #MemMap(). Any backing memory allocations will be freed if there are no existing mappings and there are no unreleased memory
handles.
When #MemUnmap() returns successfully the address range is converted to an address reservation and can be used for a future calls to #MemMap(). Any
new mapping to this virtual address will need to have access granted through #MemSetAccess(), as all mappings start with no accessibility setup.
""",
CUdeviceptr("ptr", "starting address for the virtual address range to unmap"),
size_t("size", "size of the virtual address range to unmap")
)
IgnoreMissing..CUresult(
"MemSetAccess",
"""
Set the access flags for each location specified in {@code desc} for the given virtual address range.
Given the virtual address range via {@code ptr} and {@code size}, and the locations in the array given by {@code desc} and {@code count}, set the
access flags for the target locations. The range must be a fully mapped address range containing all allocations created by #MemMap() / #MemCreate().
""",
CUdeviceptr("ptr", "starting address for the virtual address range"),
size_t("size", "length of the virtual address range"),
CUmemAccessDesc.const.p("desc", "array of {@code CUmemAccessDesc} that describe how to change the - mapping for each location specified"),
AutoSize("desc")..size_t("count", "number of {@code CUmemAccessDesc} in {@code desc}")
)
IgnoreMissing..CUresult(
"MemGetAccess",
"Get the access {@code flags} set for the given {@code location} and {@code ptr}.",
Check(1)..unsigned_long_long.p("flags", "flags set for this location"),
CUmemLocation.const.p("location", "location in which to check the flags for"),
CUdeviceptr("ptr", "address in which to check the access flags for")
)
IgnoreMissing..CUresult(
"MemExportToShareableHandle",
"""
Exports an allocation to a requested shareable handle type.
Given a CUDA memory handle, create a shareable memory allocation handle that can be used to share the memory with other processes. The recipient
process can convert the shareable handle back into a CUDA memory handle using #MemImportFromShareableHandle() and map it with #MemMap(). The
implementation of what this handle is and how it can be transferred is defined by the requested handle type in {@code handleType}.
Once all shareable handles are closed and the allocation is released, the allocated memory referenced will be released back to the OS and uses of the
CUDA handle afterward will lead to undefined behavior.
This API can also be used in conjunction with other APIs (e.g. Vulkan, OpenGL) that support importing memory from the shareable type
""",
MultiType(PointerMapping.DATA_POINTER)..Unsafe..void.p("shareableHandle", "pointer to the location in which to store the requested handle type"),
CUmemGenericAllocationHandle("handle", "CUDA handle for the memory allocation"),
CUmemAllocationHandleType("handleType", "type of shareable handle requested (defines type and size of the {@code shareableHandle} output parameter)"),
unsigned_long_long("flags", "reserved, must be zero")
)
IgnoreMissing..CUresult(
"MemImportFromShareableHandle",
"""
Imports an allocation from a requested shareable handle type.
If the current process cannot support the memory described by this shareable handle, this API will error as #CUDA_ERROR_NOT_SUPPORTED.
${note("""
Importing shareable handles exported from some graphics APIs(VUlkan, OpenGL, etc) created on devices under an SLI group may not be supported, and thus
this API will return #CUDA_ERROR_NOT_SUPPORTED. There is no guarantee that the contents of {@code handle} will be the same CUDA memory handle for the
same given OS shareable handle, or the same underlying allocation.
""")}
""",
Check(1)..CUmemGenericAllocationHandle.p("handle", "CUDA Memory handle for the memory allocation"),
opaque_p("osHandle", "shareable Handle representing the memory allocation that is to be imported"),
CUmemAllocationHandleType("shHandleType", "handle type of the exported handle {@code CUmemAllocationHandleType}")
)
IgnoreMissing..CUresult(
"MemGetAllocationGranularity",
"""
Calculates either the minimal or recommended granularity.
Calculates either the minimal or recommended granularity for a given allocation specification and returns it in granularity. This granularity can be
used as a multiple for alignment, size, or address mapping.
""",
Check(1)..size_t.p("granularity", "returned granularity"),
CUmemAllocationProp.const.p("prop", "property for which to determine the granularity for"),
CUmemAllocationGranularity_flags("option", "determines which granularity to return")
)
IgnoreMissing..CUresult(
"MemGetAllocationPropertiesFromHandle",
"Retrieve the contents of the property structure defining properties for this handle.",
CUmemAllocationProp.p("prop", "pointer to a properties structure which will hold the information about this handle"),
CUmemGenericAllocationHandle("handle", "handle which to perform the query on")
)
IgnoreMissing..CUresult(
"MemRetainAllocationHandle",
"""
Given an address {@code addr}, returns the allocation handle of the backing memory allocation.
The handle is guaranteed to be the same handle value used to map the memory. If the address requested is not mapped, the function will fail. The
returned handle must be released with corresponding number of calls to #MemRelease().
${note("""The address {@code addr}, can be any address in a range previously mapped by #MemMap(), and not necessarily the start address.""")}
""",
Check(1)..CUmemGenericAllocationHandle.p("handle", "CUDA Memory handle for the backing memory allocation"),
Unsafe..void.p("addr", "memory address to query, that has been mapped previously")
)
IgnoreMissing..CUresult(
"MemFreeAsync",
"""
Frees memory with stream ordered semantics.
Inserts a free operation into {@code hStream}. The allocation must not be accessed after stream execution reaches the free. After this API returns,
accessing the memory from any subsequent work launched on the GPU or querying its pointer attributes results in undefined behavior.
${note("""During stream capture, this function results in the creation of a free node and must therefore be passed the address of a graph
allocation.""")}
""",
CUdeviceptr("dptr", "memory to free"),
nullable..CUstream("hStream", "the stream establishing the stream ordering contract")
).ptsz()
IgnoreMissing..CUresult(
"MemAllocAsync",
"""
Allocates memory with stream ordered semantics
Inserts an allocation operation into {@code hStream}. A pointer to the allocated memory is returned immediately in {@code *dptr}. The allocation must
not be accessed until the the allocation operation completes. The allocation comes from the memory pool current to the stream's device.
${note("""The default memory pool of a device contains device memory from that device.""")}
${note("""Basic stream ordering allows future work submitted into the same stream to use the allocation. Stream query, stream synchronize, and CUDA
events can be used to guarantee that the allocation operation completes before work submitted in a separate stream runs.""")}
${note("""During stream capture, this function results in the creation of an allocation node. In this case, the allocation is owned by the graph
instead of the memory pool. The memory pool's properties are used to set the node's creation parameters.""")}
""",
Check(1)..CUdeviceptr.p("dptr", "returned device pointer"),
size_t("bytesize", "number of bytes to allocate"),
nullable..CUstream("hStream", "the stream establishing the stream ordering contract and the memory pool to allocate from")
).ptsz()
IgnoreMissing..CUresult(
"MemPoolTrimTo",
"""
Tries to release memory back to the OS.
Releases memory back to the OS until the pool contains fewer than {@code minBytesToKeep} reserved bytes, or there is no more memory that the allocator
can safely release. The allocator cannot release OS allocations that back outstanding asynchronous allocations. The OS allocations may happen at
different granularity from the user allocations.
${note("""Allocations that have not been freed count as outstanding.""")}
${note("""Allocations that have been asynchronously freed but whose completion has not been observed on the host (eg. by a synchronize) can count as
outstanding.""")}
""",
CUmemoryPool("pool", "the memory pool to trim"),
size_t(
"minBytesToKeep",
"""
if the pool has less than {@code minBytesToKeep} reserved, the {@code TrimTo} operation is a no-op. Otherwise the pool will be guaranteed to have
at least {@code minBytesToKeep} bytes reserved after the operation.
"""
)
)
IgnoreMissing..CUresult(
"MemPoolSetAttribute",
"Sets attributes of a memory pool.",
CUmemoryPool("pool", "the memory pool to modify"),
CUmemPool_attribute("attr", "the attribute to modify", "MEMPOOL_ATTR_\\w+"),
MultiType(PointerMapping.DATA_INT, PointerMapping.DATA_LONG)..Unsafe..void.p("value", "pointer to the value to assign")
)
IgnoreMissing..CUresult(
"MemPoolGetAttribute",
"Gets attributes of a memory pool.",
CUmemoryPool("pool", "the memory pool to get attributes of"),
CUmemPool_attribute("attr", "the attribute to get", "MEMPOOL_ATTR_\\w+"),
MultiType(PointerMapping.DATA_INT, PointerMapping.DATA_LONG)..Unsafe..void.p("value", "retrieved value")
)
IgnoreMissing..CUresult(
"MemPoolSetAccess",
"Controls visibility of pools between devices.",
CUmemoryPool("pool", "the pool being modified"),
CUmemAccessDesc.const.p("map", "array of access descriptors. Each descriptor instructs the access to enable for a single gpu."),
AutoSize("map")..size_t("count", "number of descriptors in the map array")
)
IgnoreMissing..CUresult(
"MemPoolGetAccess",
"""
Returns the accessibility of a pool from a device.
Returns the accessibility of the pool's memory from the specified location.
""",
Check(1)..CUmemAccess_flags.p("flags", "the accessibility of the pool from the specified location"),
CUmemoryPool("memPool", "the pool being queried"),
CUmemLocation.p("location", "the location accessing the pool")
)
IgnoreMissing..CUresult(
"MemPoolCreate",
"""
Creates a memory pool.
Creates a CUDA memory pool and returns the handle in {@code pool}. The {@code poolProps} determines the properties of the pool such as the backing
device and IPC capabilities.
By default, the pool's memory will be accessible from the device it is allocated on.
${note("""Specifying #MEM_HANDLE_TYPE_NONE creates a memory pool that will not support IPC.""")}
""",
Check(1)..CUmemoryPool.p("pool", ""),
CUmemPoolProps.const.p("poolProps", "")
)
IgnoreMissing..CUresult(
"MemPoolDestroy",
"""
Destroys the specified memory pool.
If any pointers obtained from this pool haven't been freed or the pool has free operations that haven't completed when #MemPoolDestroy() is invoked,
the function will return immediately and the resources associated with the pool will be released automatically once there are no more outstanding
allocations.
Destroying the current mempool of a device sets the default mempool of that device as the current mempool for that device.
${note("""A device's default memory pool cannot be destroyed.""")}
""",
CUmemoryPool("pool", "")
)
IgnoreMissing..CUresult(
"MemAllocFromPoolAsync",
"""
Allocates memory from a specified pool with stream ordered semantics.
Inserts an allocation operation into {@code hStream}. A pointer to the allocated memory is returned immediately in {@code *dptr}. The allocation must
not be accessed until the the allocation operation completes. The allocation comes from the specified memory pool.
${note("""The specified memory pool may be from a device different than that of the specified {@code hStream}.""")}
${ul(
"""
Basic stream ordering allows future work submitted into the same stream to use the allocation. Stream query, stream synchronize, and CUDA events
can be used to guarantee that the allocation operation completes before work submitted in a separate stream runs.
"""
)}
${note("""During stream capture, this function results in the creation of an allocation node. In this case, the allocation is owned by the graph
instead of the memory pool. The memory pool's properties are used to set the node's creation parameters.""")}
""",
Check(1)..CUdeviceptr.p("dptr", "returned device pointer"),
size_t("bytesize", "number of bytes to allocate"),
CUmemoryPool("pool", "the pool to allocate from"),
nullable..CUstream("hStream", "the stream establishing the stream ordering semantic")
).ptsz()
IgnoreMissing..CUresult(
"MemPoolExportToShareableHandle",
"""
Exports a memory pool to the requested handle type.
Given an IPC capable mempool, create an OS handle to share the pool with another process. A recipient process can convert the shareable handle into a
mempool with #MemPoolImportFromShareableHandle(). Individual pointers can then be shared with the #MemPoolExportPointer() and
#MemPoolImportPointer() APIs. The implementation of what the shareable handle is and how it can be transferred is defined by the requested handle
type.
${note("""To create an IPC capable mempool, create a mempool with a {@code CUmemAllocationHandleType} other than #MEM_HANDLE_TYPE_NONE.""")}
""",
MultiType(PointerMapping.DATA_POINTER)..Unsafe..void.p("handle_out", "returned OS handle"),
CUmemoryPool("pool", "pool to export"),
CUmemAllocationHandleType("handleType", "the type of handle to create"),
unsigned_long_long("flags", "must be 0")
)
IgnoreMissing..CUresult(
"MemPoolImportFromShareableHandle",
"""
Imports a memory pool from a shared handle.
Specific allocations can be imported from the imported pool with #MemPoolImportPointer().
${note("""Imported memory pools do not support creating new allocations. As such imported memory pools may not be used in #DeviceSetMemPool() or
#MemAllocFromPoolAsync() calls.""")}
""",
Check(1)..CUmemoryPool.p("pool_out", "returned memory pool"),
MultiType(PointerMapping.DATA_POINTER)..Unsafe..void.p("handle", "OS handle of the pool to open"),
CUmemAllocationHandleType("handleType", "the type of handle being imported"),
unsigned_long_long("flags", "must be 0")
)
IgnoreMissing..CUresult(
"MemPoolExportPointer",
"""
Export data to share a memory pool allocation between processes.
Constructs {@code shareData_out} for sharing a specific allocation from an already shared memory pool. The recipient process can import the allocation
with the #MemPoolImportPointer() api. The data is not a handle and may be shared through any IPC mechanism.
""",
CUmemPoolPtrExportData.p("shareData_out", "returned export data"),
CUdeviceptr("ptr", "pointer to memory being exported")
)
IgnoreMissing..CUresult(
"MemPoolImportPointer",
"""
Import a memory pool allocation from another process.
Returns in {@code ptr_out} a pointer to the imported memory. The imported memory must not be accessed before the allocation operation completes in the
exporting process. The imported memory must be freed from all importing processes before being freed in the exporting process. The pointer may be freed
with #MemFree() or #MemFreeAsync(). If {@code cuMemFreeAsync} is used, the free must be completed on the importing process before the free operation on
the exporting process.
${note("""The {@code cuMemFreeAsync} api may be used in the exporting process before the cuMemFreeAsync operation completes in its stream as long as
the {@code cuMemFreeAsync} in the exporting process specifies a stream with a stream dependency on the importing process's {@code cuMemFreeAsync}.""")}
""",
Check(1)..CUdeviceptr.p("ptr_out", "pointer to imported memory"),
CUmemoryPool("pool", "pool from which to import"),
CUmemPoolPtrExportData.p("shareData", "data specifying the memory to import")
)
IgnoreMissing..CUresult(
"PointerGetAttribute",
"""
Returns information about a pointer.
The supported attributes are:
${ul(
"""
#POINTER_ATTRIBUTE_CONTEXT: Returns in {@code *data} the {@code CUcontext} in which {@code ptr} was allocated or registered. The type of
{@code data} must be {@code CUcontext *}.
If {@code ptr} was not allocated by, mapped by, or registered with a {@code CUcontext} which uses unified virtual addressing then
#CUDA_ERROR_INVALID_VALUE is returned.
""",
"""
#POINTER_ATTRIBUTE_MEMORY_TYPE:
Returns in {@code *data} the physical memory type of the memory that {@code ptr} addresses as a {@code CUmemorytype} enumerated value. The type of
{@code data} must be unsigned int.
If {@code ptr} addresses device memory then {@code *data} is set to #MEMORYTYPE_DEVICE. The particular {@code CUdevice} on which the memory resides
is the {@code CUdevice} of the {@code CUcontext} returned by the #POINTER_ATTRIBUTE_CONTEXT attribute of {@code ptr}.
If {@code ptr} addresses host memory then {@code *data} is set to #MEMORYTYPE_HOST.
If {@code ptr} was not allocated by, mapped by, or registered with a {@code CUcontext} which uses unified virtual addressing then
#CUDA_ERROR_INVALID_VALUE is returned.
If the current {@code CUcontext} does not support unified virtual addressing then #CUDA_ERROR_INVALID_CONTEXT is returned.
""",
"""
#POINTER_ATTRIBUTE_DEVICE_POINTER: Returns in {@code *data} the device pointer value through which {@code ptr} may be accessed by kernels running
in the current {@code CUcontext}. The type of {@code data} must be {@code CUdeviceptr *}.
If there exists no device pointer value through which kernels running in the current {@code CUcontext} may access {@code ptr} then
#CUDA_ERROR_INVALID_VALUE is returned.
If there is no current {@code CUcontext} then #CUDA_ERROR_INVALID_CONTEXT is returned.
Except in the exceptional disjoint addressing cases discussed below, the value returned in {@code *data} will equal the input value {@code ptr}.
""",
"""
#POINTER_ATTRIBUTE_HOST_POINTER: Returns in {@code *data} the host pointer value through which {@code ptr} may be accessed by by the host program.
The type of {@code data} must be {@code void **}. If there exists no host pointer value through which the host program may directly access
{@code ptr} then #CUDA_ERROR_INVALID_VALUE is returned.
Except in the exceptional disjoint addressing cases discussed below, the value returned in {@code *data} will equal the input value {@code ptr}.
""",
"""
#POINTER_ATTRIBUTE_P2P_TOKENS: Returns in {@code *data} two tokens for use with the nv-p2p.h Linux kernel interface. {@code data} must be a struct
of type ##CUDA_POINTER_ATTRIBUTE_P2P_TOKENS.
{@code ptr} must be a pointer to memory obtained from #MemAlloc(). Note that {@code p2pToken} and {@code vaSpaceToken} are only valid for the
lifetime of the source allocation. A subsequent allocation at the same address may return completely different tokens. Querying this attribute has
a side effect of setting the attribute #POINTER_ATTRIBUTE_SYNC_MEMOPS for the region of memory that {@code ptr} points to.
""",
"""
#POINTER_ATTRIBUTE_SYNC_MEMOPS:
A boolean attribute which when set, ensures that synchronous memory operations initiated on the region of memory that {@code ptr} points to will
always synchronize. See further documentation in the section titled "API synchronization behavior" to learn more about cases when synchronous
memory operations can exhibit asynchronous behavior.
""",
"""
#POINTER_ATTRIBUTE_BUFFER_ID: Returns in {@code *data} a buffer ID which is guaranteed to be unique within the process. {@code data} must point to
an unsigned long long.
{@code ptr} must be a pointer to memory obtained from a CUDA memory allocation API. Every memory allocation from any of the CUDA memory allocation
APIs will have a unique ID over a process lifetime. Subsequent allocations do not reuse IDs from previous freed allocations. IDs are only unique
within a single process.
""",
"""
#POINTER_ATTRIBUTE_IS_MANAGED: Returns in {@code *data} a boolean that indicates whether the pointer points to managed memory or not.
If {@code ptr} is not a valid CUDA pointer then #CUDA_ERROR_INVALID_VALUE is returned.
""",
"""
#POINTER_ATTRIBUTE_DEVICE_ORDINAL: Returns in {@code *data} an integer representing a device ordinal of a device against which the memory was
allocated or registered.
""",
"""
#POINTER_ATTRIBUTE_IS_LEGACY_CUDA_IPC_CAPABLE: Returns in {@code *data} a boolean that indicates if this pointer maps to an allocation that is
suitable for {@code cudaIpcGetMemHandle()}.
""",
"""
#POINTER_ATTRIBUTE_RANGE_START_ADDR: Returns in {@code *data} the starting address for the allocation referenced by the device pointer {@code ptr}.
Note that this is not necessarily the address of the mapped region, but the address of the mappable address range {@code ptr} references (e.g. from
#MemAddressReserve()).
""",
"""
#POINTER_ATTRIBUTE_RANGE_SIZE: Returns in {@code *data} the size for the allocation referenced by the device pointer {@code ptr}. Note that this is
not necessarily the size of the mapped region, but the size of the mappable address range {@code ptr} references (e.g. from #MemAddressReserve()).
To retrieve the size of the mapped region, see #MemGetAddressRange().
""",
"""
#POINTER_ATTRIBUTE_MAPPED: Returns in {@code *data} a boolean that indicates if this pointer is in a valid address range that is mapped to a
backing allocation.
""",
"""
#POINTER_ATTRIBUTE_ALLOWED_HANDLE_TYPES: Returns a bitmask of the allowed handle types for an allocation that may be passed to
#MemExportToShareableHandle().
""",
"#POINTER_ATTRIBUTE_MEMPOOL_HANDLE: Returns in {@code *data} the handle to the mempool that the allocation was obtained from."
)}
Note that for most allocations in the unified virtual address space the host and device pointer for accessing the allocation will be the same. The
exceptions to this are - user memory registered using #MemHostRegister() - host memory allocated using #MemHostAlloc() with the
#MEMHOSTALLOC_WRITECOMBINED flag For these types of allocation there will exist separate, disjoint host and device addresses for accessing the
allocation. In particular
${ul(
"The host address will correspond to an invalid unmapped device address (which will result in an exception if accessed from the device)",
"The device address will correspond to an invalid unmapped host address (which will result in an exception if accessed from the host)."
)}
For these types of allocations, querying #POINTER_ATTRIBUTE_HOST_POINTER and #POINTER_ATTRIBUTE_DEVICE_POINTER may be used to retrieve the host and
device addresses from either address.
""",
MultiType(
PointerMapping.DATA_POINTER,
PointerMapping.DATA_INT,
PointerMapping.DATA_LONG
)..Unsafe..void.p("data", "returned pointer attribute value"),
CUpointer_attribute("attribute", "pointer attribute to query"),
CUdeviceptr("ptr", "pointer")
)
IgnoreMissing..CUresult(
"MemPrefetchAsync",
"""
Prefetches memory to the specified destination device,
Prefetches memory to the specified destination device. {@code devPtr} is the base device pointer of the memory to be prefetched and {@code dstDevice}
is the destination device. {@code count} specifies the number of bytes to copy. {@code hStream} is the stream in which the operation is enqueued. The
memory range must refer to managed memory allocated via #MemAllocManaged() or declared via __managed__ variables.
Passing in #DEVICE_CPU for {@code dstDevice} will prefetch the data to host memory. If {@code dstDevice} is a GPU, then the device attribute
#DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS must be non-zero. Additionally, {@code hStream} must be associated with a device that has a non-zero
value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS.
The start address and end address of the memory range will be rounded down and rounded up respectively to be aligned to CPU page size before the
prefetch operation is enqueued in the stream.
If no physical memory has been allocated for this region, then this memory region will be populated and mapped on the destination device. If there's
insufficient memory to prefetch the desired region, the Unified Memory driver may evict pages from other #MemAllocManaged() allocations to host memory
in order to make room. Device memory allocated using #MemAlloc() or #ArrayCreate() will not be evicted.
By default, any mappings to the previous location of the migrated pages are removed and mappings for the new location are only setup on {@code
dstDevice}. The exact behavior however also depends on the settings applied to this memory range via #MemAdvise() as described below:
If #MEM_ADVISE_SET_READ_MOSTLY was set on any subset of this memory range, then that subset will create a read-only copy of the pages on {@code
dstDevice}.
If #MEM_ADVISE_SET_PREFERRED_LOCATION was called on any subset of this memory range, then the pages will be migrated to {@code dstDevice} even if
{@code dstDevice} is not the preferred location of any pages in the memory range.
If #MEM_ADVISE_SET_ACCESSED_BY was called on any subset of this memory range, then mappings to those pages from all the appropriate processors are
updated to refer to the new location if establishing such a mapping is possible. Otherwise, those mappings are cleared.
Note that this API is not required for functionality and only serves to improve performance by allowing the application to migrate data to a suitable
location before it is accessed. Memory accesses to this range are always coherent and are allowed even when the data is actively being migrated.
Note that this function is asynchronous with respect to the host and all work on other devices.
""",
CUdeviceptr("devPtr", "pointer to be prefetched"),
size_t("count", "size in bytes"),
CUdevice("dstDevice", "destination device to prefetch to"),
nullable..CUstream("hStream", "stream to enqueue prefetch operation")
).ptsz()
IgnoreMissing..CUresult(
"MemAdvise",
"""
Advise about the usage of a given memory range.
Advise the Unified Memory subsystem about the usage pattern for the memory range starting at {@code devPtr} with a size of {@code count} bytes. The
start address and end address of the memory range will be rounded down and rounded up respectively to be aligned to CPU page size before the advice is
applied. The memory range must refer to managed memory allocated via #MemAllocManaged() or declared via __managed__ variables. The memory range
could also refer to system-allocated pageable memory provided it represents a valid, host-accessible region of memory and all additional constraints
imposed by {@code advice} as outlined below are also satisfied. Specifying an invalid system-allocated pageable memory range results in an error being
returned.
The {@code advice} parameter can take the following values:
${ul(
"""
#MEM_ADVISE_SET_READ_MOSTLY: This implies that the data is mostly going to be read from and only occasionally written to. Any read accesses
from any processor to this region will create a read-only copy of at least the accessed pages in that processor's memory. Additionally, if
#MemPrefetchAsync() is called on this region, it will create a read-only copy of the data on the destination processor. If any processor writes to
this region, all copies of the corresponding page will be invalidated except for the one where the write occurred. The {@code device} argument is
ignored for this advice. Note that for a page to be read-duplicated, the accessing processor must either be the CPU or a GPU that has a non-zero
value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS. Also, if a context is created on a device that does not have the
device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS set, then read-duplication will not occur until all such contexts are destroyed.
If the memory region refers to valid system-allocated pageable memory, then the accessing device must have a non-zero value for the device
attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS for a read-only copy to be created on that device. Note however that if the accessing device
also has a non-zero value for the device attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLES, then setting this advice
will not create a read-only copy when that device accesses this memory region.
""",
"""
#MEM_ADVISE_UNSET_READ_MOSTLY: Undoes the effect of #MEM_ADVISE_SET_READ_MOSTLY and also prevents the Unified Memory driver from attempting
heuristic read-duplication on the memory range. Any read-duplicated copies of the data will be collapsed into a single copy. The location for the
collapsed copy will be the preferred location if the page has a preferred location and one of the read-duplicated copies was resident at that
location. Otherwise, the location chosen is arbitrary.
""",
"""
#MEM_ADVISE_SET_PREFERRED_LOCATION: This advice sets the preferred location for the data to be the memory belonging to {@code device}. Passing
in CU_DEVICE_CPU for {@code device} sets the preferred location as host memory. If {@code device} is a GPU, then it must have a non-zero value for
the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS. Setting the preferred location does not cause data to migrate to that
location immediately. Instead, it guides the migration policy when a fault occurs on that memory region. If the data is already in its preferred
location and the faulting processor can establish a mapping without requiring the data to be migrated, then data migration will be avoided. On the
other hand, if the data is not in its preferred location or if a direct mapping cannot be established, then it will be migrated to the processor
accessing it. It is important to note that setting the preferred location does not prevent data prefetching done using #MemPrefetchAsync(). Having
a preferred location can override the page thrash detection and resolution logic in the Unified Memory driver. Normally, if a page is detected to
be constantly thrashing between for example host and device memory, the page may eventually be pinned to host memory by the Unified Memory driver.
But if the preferred location is set as device memory, then the page will continue to thrash indefinitely. If #MEM_ADVISE_SET_READ_MOSTLY is
also set on this memory region or any subset of it, then the policies associated with that advice will override the policies of this advice, unless
read accesses from {@code device} will not result in a read-only copy being created on that device as outlined in description for the advice
#MEM_ADVISE_SET_READ_MOSTLY. If the memory region refers to valid system-allocated pageable memory, then {@code device} must have a non-zero
value for the device attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS. Additionally, if {@code device} has a non-zero value for the device
attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLES, then this call has no effect. Note however that this behavior may
change in the future.
""",
"#MEM_ADVISE_UNSET_PREFERRED_LOCATION: Undoes the effect of #MEM_ADVISE_SET_PREFERRED_LOCATION and changes the preferred location to none.",
"""
#MEM_ADVISE_SET_ACCESSED_BY: This advice implies that the data will be accessed by {@code device}. Passing in #DEVICE_CPU for {@code
device} will set the advice for the CPU. If {@code device} is a GPU, then the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS must
be non-zero. This advice does not cause data migration and has no impact on the location of the data per se. Instead, it causes the data to always
be mapped in the specified processor's page tables, as long as the location of the data permits a mapping to be established. If the data gets
migrated for any reason, the mappings are updated accordingly. This advice is recommended in scenarios where data locality is not important, but
avoiding faults is. Consider for example a system containing multiple GPUs with peer-to-peer access enabled, where the data located on one GPU is
occasionally accessed by peer GPUs. In such scenarios, migrating data over to the other GPUs is not as important because the accesses are
infrequent and the overhead of migration may be too high. But preventing faults can still help improve performance, and so having a mapping set up
in advance is useful. Note that on CPU access of this data, the data may be migrated to host memory because the CPU typically cannot access device
memory directly. Any GPU that had the #MEM_ADVISE_SET_ACCESSED_BY flag set for this data will now have its mapping updated to point to the page
in host memory. If #MEM_ADVISE_SET_READ_MOSTLY is also set on this memory region or any subset of it, then the policies associated with that
advice will override the policies of this advice. Additionally, if the preferred location of this memory region or any subset of it is also {@code
device}, then the policies associated with #MEM_ADVISE_SET_PREFERRED_LOCATION will override the policies of this advice. If the memory region
refers to valid system-allocated pageable memory, then {@code device} must have a non-zero value for the device attribute
#DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS. Additionally, if {@code device} has a non-zero value for the device attribute
#DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLES, then this call has no effect.
""",
"""
#MEM_ADVISE_UNSET_ACCESSED_BY: Undoes the effect of #MEM_ADVISE_SET_ACCESSED_BY. Any mappings to the data from {@code device} may be
removed at any time causing accesses to result in non-fatal page faults. If the memory region refers to valid system-allocated pageable memory,
then {@code device} must have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS. Additionally, if {@code
device} has a non-zero value for the device attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS_USES_HOST_PAGE_TABLES, then this call has no
effect.
"""
)}
""",
CUdeviceptr("devPtr", "pointer to memory to set the advice for"),
size_t("count", "size in bytes of the memory range"),
CUmem_advise("advice", "advice to be applied for the specified memory range"),
CUdevice("device", "device to apply the advice for")
)
IgnoreMissing..CUresult(
"MemRangeGetAttribute",
"""
Query an attribute of a given memory range.
Query an attribute about the memory range starting at {@code devPtr} with a size of {@code count} bytes. The memory range must refer to managed memory
allocated via #MemAllocManaged() or declared via __managed__ variables.
The {@code attribute} parameter can take the following values:
${ul(
"""
#MEM_RANGE_ATTRIBUTE_READ_MOSTLY: If this attribute is specified, {@code data} will be interpreted as a 32-bit integer, and {@code dataSize}
must be 4. The result returned will be 1 if all pages in the given memory range have read-duplication enabled, or 0 otherwise.
""",
"""
#MEM_RANGE_ATTRIBUTE_PREFERRED_LOCATION: If this attribute is specified, {@code data} will be interpreted as a 32-bit integer, and {@code
dataSize} must be 4. The result returned will be a GPU device id if all pages in the memory range have that GPU as their preferred location, or it
will be CU_DEVICE_CPU if all pages in the memory range have the CPU as their preferred location, or it will be CU_DEVICE_INVALID if either all the
pages don't have the same preferred location or some of the pages don't have a preferred location at all. Note that the actual location of the
pages in the memory range at the time of the query may be different from the preferred location.
""",
"""
#MEM_RANGE_ATTRIBUTE_ACCESSED_BY: If this attribute is specified, {@code data} will be interpreted as an array of 32-bit integers, and {@code
dataSize} must be a non-zero multiple of 4. The result returned will be a list of device ids that had #MEM_ADVISE_SET_ACCESSED_BY set for that
entire memory range. If any device does not have that advice set for the entire memory range, that device will not be included. If {@code data} is
larger than the number of devices that have that advice set for that memory range, CU_DEVICE_INVALID will be returned in all the extra space
provided. For ex., if {@code dataSize} is 12 (i.e. {@code data} has 3 elements) and only device 0 has the advice set, then the result returned will
be { 0, CU_DEVICE_INVALID, CU_DEVICE_INVALID }. If {@code data} is smaller than the number of devices that have that advice set, then only as many
devices will be returned as can fit in the array. There is no guarantee on which specific devices will be returned, however.
""",
"""
#MEM_RANGE_ATTRIBUTE_LAST_PREFETCH_LOCATION: If this attribute is specified, {@code data} will be interpreted as a 32-bit integer, and {@code
dataSize} must be 4. The result returned will be the last location to which all pages in the memory range were prefetched explicitly via
#MemPrefetchAsync(). This will either be a GPU id or CU_DEVICE_CPU depending on whether the last location for prefetch was a GPU or the CPU
respectively. If any page in the memory range was never explicitly prefetched or if all pages were not prefetched to the same location,
CU_DEVICE_INVALID will be returned. Note that this simply returns the last location that the applicaton requested to prefetch the memory range to.
It gives no indication as to whether the prefetch operation to that location has completed or even begun.
"""
)}
""",
void.p("data", "a pointers to a memory location where the result of each attribute query will be written to"),
AutoSize("data")..size_t("dataSize", "the size of {@code data}"),
CUmem_range_attribute("attribute", "the attribute to query"),
CUdeviceptr("devPtr", "start of the range to query"),
size_t("count", "size of the range to query")
)
IgnoreMissing..CUresult(
"MemRangeGetAttributes",
"""
Query attributes of a given memory range.
Query attributes of the memory range starting at {@code devPtr} with a size of {@code count} bytes. The memory range must refer to managed memory
allocated via #MemAllocManaged() or declared via __managed__ variables. The {@code attributes} array will be interpreted to have {@code numAttributes}
entries. The {@code dataSizes} array will also be interpreted to have {@code numAttributes} entries. The results of the query will be stored in {@code data}.
The list of supported attributes are given below. Please refer to #MemRangeGetAttribute() for attribute descriptions and restrictions.
${ul(
"#MEM_RANGE_ATTRIBUTE_READ_MOSTLY",
"#MEM_RANGE_ATTRIBUTE_PREFERRED_LOCATION",
"#MEM_RANGE_ATTRIBUTE_ACCESSED_BY",
"#MEM_RANGE_ATTRIBUTE_LAST_PREFETCH_LOCATION"
)}
""",
void.p.p("data", "a two-dimensional array containing pointers to memory locations where the result of each attribute query will be written to"),
size_t.p("dataSizes", "array containing the sizes of each result"),
CUmem_range_attribute.p(
"attributes",
"an array of attributes to query (numAttributes and the number of attributes in this array should match)",
"MEM_RANGE_ATTRIBUTE_\\w+"),
AutoSize(
"attributes",
"data",
"dataSizes"
)..size_t("numAttributes", "number of attributes to query"),
CUdeviceptr("devPtr", "start of the range to query"),
size_t("count", "size of the range to query")
)
IgnoreMissing..CUresult(
"PointerSetAttribute",
"""
Set attributes on a previously allocated memory region.
The supported attributes are:
${ul(
"""
#POINTER_ATTRIBUTE_SYNC_MEMOPS: A boolean attribute that can either be set (1) or unset (0).
When set, the region of memory that {@code ptr} points to is guaranteed to always synchronize memory operations that are synchronous. If there are
some previously initiated synchronous memory operations that are pending when this attribute is set, the function does not return until those
memory operations are complete. See further documentation in the section titled "API synchronization behavior" to learn more about cases when
synchronous memory operations can exhibit asynchronous behavior. {@code value} will be considered as a pointer to an unsigned integer to which this
attribute is to be set.
"""
)}
""",
Unsafe..void.const.p("value", "pointer to memory containing the value to be set"),
CUpointer_attribute("attribute", "pointer attribute to set"),
CUdeviceptr("ptr", "pointer to a memory region allocated using CUDA memory allocation APIs")
)
IgnoreMissing..CUresult(
"PointerGetAttributes",
"""
Returns information about a pointer.
Unlike #PointerGetAttribute(), this function will not return an error when the {@code ptr} encountered is not a valid CUDA pointer. Instead, the
attributes are assigned default #NULL values and #CUDA_SUCCESS is returned.
If {@code ptr} was not allocated by, mapped by, or registered with a {@code CUcontext} which uses UVA (Unified Virtual Addressing),
#CUDA_ERROR_INVALID_CONTEXT is returned.
""",
AutoSize("attributes", "data")..unsigned_int("numAttributes", "number of attributes to query"),
CUpointer_attribute.p(
"attributes", "an array of attributes to query (numAttributes and the number of attributes in this array should match)",
"POINTER_ATTRIBUTE_\\w+"
),
void.p.p("data", "a two-dimensional array containing pointers to memory locations where the result of each attribute query will be written to"),
CUdeviceptr("ptr", "pointer to query")
)
CUresult(
"StreamCreate",
"""
Create a stream.
Creates a stream and returns a handle in {@code phStream}. The {@code Flags} argument determines behaviors of the stream.
Valid values for {@code Flags} are:
${ul(
"#STREAM_DEFAULT: Default stream creation flag.",
"""
#STREAM_NON_BLOCKING: Specifies that work running in the created stream may run concurrently with work in stream 0 (the NULL stream), and that
the created stream should perform no implicit synchronization with stream 0.
"""
)}
""",
Check(1)..CUstream.p("phStream", "returned newly created stream"),
unsigned_int("Flags", "parameters for stream creation")
)
CUresult(
"StreamCreateWithPriority",
"""
Create a stream with the given priority.
Creates a stream with the specified priority and returns a handle in {@code phStream}. This API alters the scheduler priority of work in the stream.
Work in a higher priority stream may preempt work already executing in a low priority stream.
{@code priority} follows a convention where lower numbers represent higher priorities. {@code 0} represents default priority. The range of meaningful
numerical priorities can be queried using #CtxGetStreamPriorityRange(). If the specified priority is outside the numerical range returned by
#CtxGetStreamPriorityRange(), it will automatically be clamped to the lowest or the highest number in the range.
${note("""Stream priorities are supported only on GPUs with compute capability 3.5 or higher.""")}
${note("""In the current implementation, only compute kernels launched in priority streams are affected by the stream's priority. Stream priorities
have no effect on host-to-device and device-to-host memory operations.""")}
""",
Check(1)..CUstream.p("phStream", "returned newly created stream"),
unsigned_int("flags", "flags for stream creation. See #StreamCreate() for a list of valid flags"),
int(
"priority",
"""
stream priority. Lower numbers represent higher priorities. See #CtxGetStreamPriorityRange() for more information about meaningful stream
priorities that can be passed.
"""
)
)
CUresult(
"StreamGetPriority",
"""
Query the priority of a given stream.
Query the priority of a stream created using #StreamCreate() or #StreamCreateWithPriority() and return the priority in {@code priority}. Note that if
the stream was created with a priority outside the numerical range returned by #CtxGetStreamPriorityRange(), this function returns the clamped
priority. See #StreamCreateWithPriority() for details about priority clamping.
""",
nullable..CUstream("hStream", "handle to the stream to be queried"),
Check(1)..int.p("priority", "pointer to a signed integer in which the stream's priority is returned")
).ptsz()
CUresult(
"StreamGetFlags",
"""
Query the flags of a given stream.
Query the flags of a stream created using #StreamCreate() or #StreamCreateWithPriority() and return the flags in {@code flags}.
""",
nullable..CUstream("hStream", "handle to the stream to be queried"),
Check(1)..unsigned_int.p(
"flags",
"""
pointer to an unsigned integer in which the stream's flags are returned The value returned in {@code flags} is a logical 'OR' of all flags that
were used while creating this stream. See #StreamCreate() for the list of valid flags.
"""
)
).ptsz()
IgnoreMissing..CUresult(
"StreamGetCtx",
"""
Query the context associated with a stream.
Returns the CUDA context that the stream is associated with.
The stream handle {@code hStream} can refer to any of the following:
${ul(
"""
a stream created via any of the CUDA driver APIs such as #StreamCreate() and #StreamCreateWithPriority(), or their runtime API equivalents such as
{@code cudaStreamCreate()}, {@code cudaStreamCreateWithFlags()} and {@code cudaStreamCreateWithPriority()}. The returned context is the context
that was active in the calling thread when the stream was created. Passing an invalid handle will result in undefined behavior.
""",
"""
any of the special streams such as the #NULL stream, #STREAM_LEGACY and #STREAM_PER_THREAD. The runtime API equivalents of these are also accepted,
which are #NULL, {@code cudaStreamLegacy()} and {@code cudaStreamPerThread()} respectively. Specifying any of the special handles will return the
context current to the calling thread. If no context is current to the calling thread, #CUDA_ERROR_INVALID_CONTEXT is returned.
"""
)}
""",
nullable..CUstream("hStream", "handle to the stream to be queried"),
Check(1)..CUcontext.p("pctx", "returned context associated with the stream")
).ptsz()
CUresult(
"StreamWaitEvent",
"""
Make a compute stream wait on an event.
Makes all future work submitted to {@code hStream} wait for all work captured in {@code hEvent}. See #EventRecord() for details on what is captured
by an event. The synchronization will be performed efficiently on the device when applicable. {@code hEvent} may be from a different context or device
than {@code hStream}.
""",
nullable..CUstream("hStream", "stream to wait"),
CUevent("hEvent", "event to wait on (may not be #NULL)", "EVENT_WAIT_\\w+"),
unsigned_int("Flags", "see {@code CUevent_capture_flags}")
).ptsz()
IgnoreMissing..CUresult(
"StreamAddCallback",
"""
Add a callback to a compute stream.
${note("""This function is slated for eventual deprecation and removal. If you do not require the callback to execute in case of a device error,
consider using #LaunchHostFunc(). Additionally, this function is not supported with #StreamBeginCapture() and #StreamEndCapture(), unlike
#LaunchHostFunc().""")}
Adds a callback to be called on the host after all currently enqueued items in the stream have completed. For each {@code cuStreamAddCallback} call,
the callback will be executed exactly once. The callback will block later work in the stream until it is finished.
The callback may be passed #CUDA_SUCCESS or an error code. In the event of a device error, all subsequently executed callbacks will receive an
appropriate {@code CUresult}.
Callbacks must not make any CUDA API calls. Attempting to use a CUDA API will result in #CUDA_ERROR_NOT_PERMITTED. Callbacks must not perform any
synchronization that may depend on outstanding device work or other callbacks that are not mandated to run earlier. Callbacks without a mandated order
(in independent streams) execute in undefined order and may be serialized.
For the purposes of Unified Memory, callback execution makes a number of guarantees:
${ul(
"""
The callback stream is considered idle for the duration of the callback. Thus, for example, a callback may always use memory attached to the
callback stream.
""",
"""
The start of execution of a callback has the same effect as synchronizing an event recorded in the same stream immediately prior to the callback.
It thus synchronizes streams which have been "joined" prior to the callback.
""",
"""
Adding device work to any stream does not have the effect of making the stream active until all preceding host functions and stream callbacks have
executed. Thus, for example, a callback might use global attached memory even if work has been added to another stream, if the work has been
ordered behind the callback with an event.
""",
"""
Completion of a callback does not cause a stream to become active except as described above. The callback stream will remain idle if no device work
follows the callback, and will remain idle across consecutive callbacks without device work in between. Thus, for example, stream synchronization
can be done by signaling from a callback at the end of the stream.
"""
)}
""",
nullable..CUstream("hStream", "stream to add callback to"),
CUstreamCallback("callback", "the function to call once preceding stream operations are complete"),
opaque_p("userData", "user specified data to be passed to the callback function"),
unsigned_int("flags", "reserved for future use, must be 0")
).ptsz()
IgnoreMissing..CUresult(
"StreamBeginCapture",
"""
Begins graph capture on a stream.
Begin graph capture on {@code hStream}. When a stream is in capture mode, all operations pushed into the stream will not be executed, but will instead
be captured into a graph, which will be returned via #StreamEndCapture(). Capture may not be initiated if {@code stream} is #STREAM_LEGACY. Capture
must be ended on the same stream in which it was initiated, and it may only be initiated if the stream is not already in capture mode. The capture mode
may be queried via #StreamIsCapturing(). A unique id representing the capture sequence may be queried via #StreamGetCaptureInfo().
${note("""Kernels captured using this API must not use texture and surface references. Reading or writing through any texture or surface reference is
undefined behavior. This restriction does not apply to texture and surface objects.""")}
""",
nullable..CUstream("hStream", "stream in which to initiate capture")
).ptsz()
IgnoreMissing..CUresult(
"StreamBeginCapture_v2",
"""
Begins graph capture on a stream.
Begin graph capture on {@code hStream}. When a stream is in capture mode, all operations pushed into the stream will not be executed, but will instead
be captured into a graph, which will be returned via #StreamEndCapture(). Capture may not be initiated if {@code stream} is #STREAM_LEGACY. Capture
must be ended on the same stream in which it was initiated, and it may only be initiated if the stream is not already in capture mode. The capture mode
may be queried via #StreamIsCapturing(). A unique id representing the capture sequence may be queried via #StreamGetCaptureInfo().
If {@code mode} is not #STREAM_CAPTURE_MODE_RELAXED, #StreamEndCapture() must be called on this stream from the same thread.
${note("""Kernels captured using this API must not use texture and surface references. Reading or writing through any texture or surface reference is
undefined behavior. This restriction does not apply to texture and surface objects.""")}
""",
nullable..CUstream("hStream", "stream in which to initiate capture"),
CUstreamCaptureMode(
"mode",
"""
controls the interaction of this capture sequence with other API calls that are potentially unsafe. For more details see
#ThreadExchangeStreamCaptureMode().
"""
)
).ptsz()
IgnoreMissing..CUresult(
"ThreadExchangeStreamCaptureMode",
"""
Swaps the stream capture interaction mode for a thread.
Sets the calling thread's stream capture interaction mode to the value contained in {@code *mode}, and overwrites {@code *mode} with the previous mode
for the thread. To facilitate deterministic behavior across function or module boundaries, callers are encouraged to use this API in a push-pop
fashion:
${codeBlock("""
CUstreamCaptureMode mode = desiredMode
cuThreadExchangeStreamCaptureMode(&mode);
...
cuThreadExchangeStreamCaptureMode(&mode); // restore previous mode""")}
During stream capture (see #StreamBeginCapture()), some actions, such as a call to {@code cudaMalloc}, may be unsafe. In the case of {@code cudaMalloc},
the operation is not enqueued asynchronously to a stream, and is not observed by stream capture. Therefore, if the sequence of operations captured via
#StreamBeginCapture() depended on the allocation being replayed whenever the graph is launched, the captured graph would be invalid.
Therefore, stream capture places restrictions on API calls that can be made within or concurrently to a #StreamBeginCapture()-#StreamEndCapture()
sequence. This behavior can be controlled via this API and flags to {@code cuStreamBeginCapture}.
A thread's mode is one of the following:
${ul(
"""
#STREAM_CAPTURE_MODE_GLOBAL: This is the default mode.
If the local thread has an ongoing capture sequence that was not initiated with #STREAM_CAPTURE_MODE_RELAXED at #StreamBeginCapture(), or if any
other thread has a concurrent capture sequence initiated with #STREAM_CAPTURE_MODE_GLOBAL, this thread is prohibited from potentially unsafe API
calls.
""",
"""
#STREAM_CAPTURE_MODE_THREAD_LOCAL: If the local thread has an ongoing capture sequence not initiated with {@code CU_STREAM_CAPTURE_MODE_RELAXED},
it is prohibited from potentially unsafe API calls. Concurrent capture sequences in other threads are ignored.
""",
"""
#STREAM_CAPTURE_MODE_RELAXED: The local thread is not prohibited from potentially unsafe API calls. Note that the thread is still prohibited from
API calls which necessarily conflict with stream capture, for example, attempting #EventQuery() on an event that was last recorded inside a capture
sequence.
"""
)}
""",
Check(1)..CUstreamCaptureMode.p("mode", "pointer to mode value to swap with the current mode")
)
IgnoreMissing..CUresult(
"StreamEndCapture",
"""
Ends capture on a stream, returning the captured graph.
End capture on {@code hStream}, returning the captured graph via {@code phGraph}. Capture must have been initiated on {@code hStream} via a call to
#StreamBeginCapture(). If capture was invalidated, due to a violation of the rules of stream capture, then a NULL graph will be returned.
If the {@code mode} argument to #StreamBeginCapture() was not #STREAM_CAPTURE_MODE_RELAXED, this call must be from the same thread as
#StreamBeginCapture().
""",
nullable..CUstream("hStream", "stream to query"),
Check(1)..CUgraph.p("phGraph", "the captured graph")
).ptsz()
IgnoreMissing..CUresult(
"StreamIsCapturing",
"""
Returns a stream's capture status.
Return the capture status of {@code hStream} via {@code captureStatus}. After a successful call, {@code *captureStatus} will contain one of the
following:
${ul(
"#STREAM_CAPTURE_STATUS_NONE: The stream is not capturing.",
"#STREAM_CAPTURE_STATUS_ACTIVE: The stream is capturing.",
"""
#STREAM_CAPTURE_STATUS_INVALIDATED: The stream was capturing but an error has invalidated the capture sequence. The capture sequence must be
terminated with #StreamEndCapture() on the stream where it was initiated in order to continue using {@code hStream}.
"""
)}
Note that, if this is called on #STREAM_LEGACY (the "null stream") while a blocking stream in the same context is capturing, it will return
#CUDA_ERROR_STREAM_CAPTURE_IMPLICIT and {@code *captureStatus} is unspecified after the call. The blocking stream capture is not invalidated.
When a blocking stream is capturing, the legacy stream is in an unusable state until the blocking stream capture is terminated. The legacy stream is
not supported for stream capture, but attempted use would have an implicit dependency on the capturing stream(s).
""",
nullable..CUstream("hStream", "stream to query"),
Check(1)..CUstreamCaptureStatus.p("captureStatus", "returns the stream's capture status")
).ptsz()
IgnoreMissing..CUresult(
"StreamGetCaptureInfo",
"""
Query capture status of a stream.
Query the capture status of a stream and and get an id for the capture sequence, which is unique over the lifetime of the process.
If called on #STREAM_LEGACY (the "null stream") while a stream not created with #STREAM_NON_BLOCKING is capturing, returns
#CUDA_ERROR_STREAM_CAPTURE_IMPLICIT.
A valid id is returned only if both of the following are true:
${ul(
"the call returns #SUCCESS",
"{@code captureStatus} is set to #STREAM_CAPTURE_STATUS_ACTIVE"
)}
""",
nullable..CUstream("hStream", ""),
Check(1)..CUstreamCaptureStatus.p("captureStatus", ""),
Check(1)..cuuint64_t.p("id", "")
).ptsz()
IgnoreMissing..CUresult(
"StreamGetCaptureInfo_v2",
"""
Query a stream's capture state (11.3+).
Query stream state related to stream capture.
If called on #STREAM_LEGACY (the "null stream") while a stream not created with #STREAM_NON_BLOCKING is capturing, returns
#CUDA_ERROR_STREAM_CAPTURE_IMPLICIT.
Valid data (other than capture status) is returned only if both of the following are true:
${ul(
"the call returns CUDA_SUCCESS",
"the returned capture status is #STREAM_CAPTURE_STATUS_ACTIVE"
)}
This version of {@code cuStreamGetCaptureInfo} is introduced in CUDA 11.3 and will supplant the previous version in 12.0. Developers requiring
compatibility across minor versions to CUDA 11.0 (driver version 445) should use #StreamGetCaptureInfo() or include a fallback path.
""",
nullable..CUstream("hStream", "the stream to query"),
Check(1)..CUstreamCaptureStatus.p("captureStatus_out", "location to return the capture status of the stream; required"),
Check(1)..nullable..cuuint64_t.p(
"id_out",
"optional location to return an id for the capture sequence, which is unique over the lifetime of the process"
),
Check(1)..nullable..CUgraph.p(
"graph_out",
"""
optional location to return the graph being captured into.
All operations other than destroy and node removal are permitted on the graph while the capture sequence is in progress. This API does not transfer
ownership of the graph, which is transferred or destroyed at #StreamEndCapture(). Note that the graph handle may be invalidated before end of
capture for certain errors. Nodes that are or become unreachable from the original stream at #StreamEndCapture() due to direct actions on the graph
do not trigger #CUDA_ERROR_STREAM_CAPTURE_UNJOINED.
"""
),
Check(1)..nullable..CUgraphNode.const.p.p(
"dependencies_out",
"""
optional location to store a pointer to an array of nodes.
The next node to be captured in the stream will depend on this set of nodes, absent operations such as event wait which modify this set. The array
pointer is valid until the next API call which operates on the stream or until end of capture. The node handles may be copied out and are valid
until they or the graph is destroyed. The driver-owned array may also be passed directly to APIs that operate on the graph (not the stream) without
copying.
"""
),
Check(1)..nullable..size_t.p("numDependencies_out", "optional location to store the size of the array returned in {@code dependencies_out}")
).ptsz()
IgnoreMissing..CUresult(
"StreamUpdateCaptureDependencies",
"""
Update the set of dependencies in a capturing stream (11.3+).
Modifies the dependency set of a capturing stream. The dependency set is the set of nodes that the next captured node in the stream will depend on.
Valid flags are #STREAM_ADD_CAPTURE_DEPENDENCIES and #STREAM_SET_CAPTURE_DEPENDENCIES. These control whether the set passed to the API is added
to the existing set or replaces it. A flags value of 0 defaults to #STREAM_ADD_CAPTURE_DEPENDENCIES.
Nodes that are removed from the dependency set via this API do not result in #CUDA_ERROR_STREAM_CAPTURE_UNJOINED if they are unreachable from the
stream at #StreamEndCapture().
Returns #CUDA_ERROR_ILLEGAL_STATE if the stream is not capturing.
This API is new in CUDA 11.3. Developers requiring compatibility across minor versions to CUDA 11.0 should not use this API or provide a fallback.
""",
nullable..CUstream("hStream", ""),
CUgraphNode.p("dependencies", ""),
AutoSize("dependencies")..size_t("numDependencies", ""),
unsigned_int("flags", "")
).ptsz()
IgnoreMissing..CUresult(
"StreamAttachMemAsync",
"""
Attach memory to a stream asynchronously.
Enqueues an operation in {@code hStream} to specify stream association of {@code length} bytes of memory starting from {@code dptr}. This function is a
stream-ordered operation, meaning that it is dependent on, and will only take effect when, previous work in stream has completed. Any previous
association is automatically replaced.
{@code dptr} must point to one of the following types of memories:
${ul(
"managed memory declared using the __managed__ keyword or allocated with #MemAllocManaged().",
"""
a valid host-accessible region of system-allocated pageable memory. This type of memory may only be specified if the device associated with the
stream reports a non-zero value for the device attribute #DEVICE_ATTRIBUTE_PAGEABLE_MEMORY_ACCESS.
"""
)}
For managed allocations, {@code length} must be either zero or the entire allocation's size. Both indicate that the entire allocation's stream
association is being changed. Currently, it is not possible to change stream association for a portion of a managed allocation.
For pageable host allocations, {@code length} must be non-zero.
The stream association is specified using {@code flags} which must be one of {@code CUmemAttach_flags}. If the #MEM_ATTACH_GLOBAL flag is specified,
the memory can be accessed by any stream on any device. If the #MEM_ATTACH_HOST flag is specified, the program makes a guarantee that it won't access
the memory on the device from any stream on a device that has a zero value for the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS. If
the #MEM_ATTACH_SINGLE flag is specified and {@code hStream} is associated with a device that has a zero value for the device attribute
#DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, the program makes a guarantee that it will only access the memory on the device from {@code hStream}.
It is illegal to attach singly to the NULL stream, because the NULL stream is a virtual global stream and not a specific stream. An error will be
returned in this case.
When memory is associated with a single stream, the Unified Memory system will allow CPU access to this memory region so long as all operations in
{@code hStream} have completed, regardless of whether other streams are active. In effect, this constrains exclusive ownership of the managed memory
region by an active GPU to per-stream activity instead of whole-GPU activity.
Accessing memory on the device from streams that are not associated with it will produce undefined results. No error checking is performed by the
Unified Memory system to ensure that kernels launched into other streams do not access this region.
It is a program's responsibility to order calls to #StreamAttachMemAsync() via events, synchronization or other means to ensure legal access to memory
at all times. Data visibility and coherency will be changed appropriately for all kernels which follow a stream-association change.
If {@code hStream} is destroyed while data is associated with it, the association is removed and the association reverts to the default visibility of
the allocation as specified at #MemAllocManaged(). For __managed__ variables, the default association is always #MEM_ATTACH_GLOBAL. Note that
destroying a stream is an asynchronous operation, and as a result, the change to default association won't happen until all work in the stream has
completed.
""",
nullable..CUstream("hStream", "stream in which to enqueue the attach operation"),
CUdeviceptr(
"dptr",
"pointer to memory (must be a pointer to managed memory or to a valid host-accessible region of system-allocated pageable memory)"
),
size_t("length", "length of memory"),
unsigned_int("flags", "must be one of {@code CUmemAttach_flags}")
).ptsz()
CUresult(
"StreamQuery",
"""
Determine status of a compute stream.
Returns #CUDA_SUCCESS if all operations in the stream specified by {@code hStream} have completed, or #CUDA_ERROR_NOT_READY if not.
For the purposes of Unified Memory, a return value of #CUDA_SUCCESS is equivalent to having called #StreamSynchronize().
""",
nullable..CUstream("hStream", "stream to query status of")
).ptsz()
CUresult(
"StreamSynchronize",
"""
Wait until a stream's tasks are completed.
Waits until the device has completed all operations in the stream specified by {@code hStream}. If the context was created with the
#CTX_SCHED_BLOCKING_SYNC flag, the CPU thread will block until the stream is finished with all of its tasks.
""",
nullable..CUstream("hStream", "stream to wait for")
).ptsz()
IgnoreMissing..CUresult(
"StreamDestroy",
"""
Destroys a stream.
Destroys the stream specified by {@code hStream}.
In case the device is still doing work in the stream {@code hStream} when #StreamDestroy() is called, the function will return immediately and the
resources associated with {@code hStream} will be released automatically once the device has completed all work in {@code hStream}.
""",
nullable..CUstream("hStream", "stream to destroy")
).versioned()
IgnoreMissing..CUresult(
"StreamCopyAttributes",
"""
Copies attributes from source stream to destination stream.
Copies attributes from source stream {@code src} to destination stream {@code dst}. Both streams must have the same context.
""",
nullable..CUstream("dst", "destination stream"),
nullable..CUstream("src", "source stream For list of attributes see {@code CUstreamAttrID}")
).ptsz()
IgnoreMissing..CUresult(
"StreamGetAttribute",
"""
Queries stream attribute.
Queries attribute {@code attr} from {@code hStream} and stores it in corresponding member of {@code value_out}.
""",
nullable..CUstream("hStream", ""),
CUstreamAttrID("attr", ""),
CUstreamAttrValue.p("value_out", "")
).ptsz()
IgnoreMissing..CUresult(
"StreamSetAttribute",
"""
Sets stream attribute.
Sets attribute {@code attr} on {@code hStream} from corresponding attribute of {@code value}. The updated attribute will be applied to subsequent work
submitted to the stream. It will not affect previously submitted work.
""",
nullable..CUstream("hStream", ""),
CUstreamAttrID("attr", ""),
CUstreamAttrValue.const.p("value", "")
).ptsz()
CUresult(
"EventCreate",
"""
Creates an event.
Creates an event {@code *phEvent} for the current context with the flags specified via {@code Flags}. Valid flags include:
${ul(
"#EVENT_DEFAULT: Default event creation flag.",
"""
#EVENT_BLOCKING_SYNC: Specifies that the created event should use blocking synchronization. A CPU thread that uses #EventSynchronize() to
wait on an event created with this flag will block until the event has actually been recorded.
""",
"""
#EVENT_DISABLE_TIMING: Specifies that the created event does not need to record timing data. Events created with this flag specified and the
#EVENT_BLOCKING_SYNC flag not specified will provide the best performance when used with #StreamWaitEvent() and #EventQuery().
""",
"""
#EVENT_INTERPROCESS: Specifies that the created event may be used as an interprocess event by #IpcGetEventHandle(). #EVENT_INTERPROCESS
must be specified along with #EVENT_DISABLE_TIMING.
"""
)}
""",
Check(1)..CUevent.p("phEvent", "returns newly created event"),
unsigned_int("Flags", "event creation flags")
)
CUresult(
"EventRecord",
"""
Records an event.
Captures in {@code hEvent} the contents of {@code hStream} at the time of this call. {@code hEvent} and {@code hStream} must be from the same context.
Calls such as #EventQuery() or #StreamWaitEvent() will then examine or wait for completion of the work that was captured. Uses of {@code hStream}
after this call do not modify {@code hEvent}. See note on default stream behavior for what is captured in the default case.
#EventRecord() can be called multiple times on the same event and will overwrite the previously captured state. Other APIs such as
#StreamWaitEvent() use the most recently captured state at the time of the API call, and are not affected by later calls to #EventRecord().
Before the first call to #EventRecord(), an event represents an empty set of work, so for example #EventQuery() would return #CUDA_SUCCESS.
""",
CUevent("hEvent", "event to record"),
nullable..CUstream("hStream", "stream to record event for")
).ptsz()
IgnoreMissing..CUresult(
"EventRecordWithFlags",
"""
Records an event.
Captures in {@code hEvent} the contents of {@code hStream} at the time of this call. {@code hEvent} and {@code hStream} must be from the same context.
Calls such as #EventQuery() or #StreamWaitEvent() will then examine or wait for completion of the work that was captured. Uses of {@code hStream}
after this call do not modify {@code hEvent}. See note on default stream behavior for what is captured in the default case.
#EventRecordWithFlags() can be called multiple times on the same event and will overwrite the previously captured state. Other APIs such as
#StreamWaitEvent() use the most recently captured state at the time of the API call, and are not affected by later calls to
#EventRecordWithFlags(). Before the first call to #EventRecordWithFlags(), an event represents an empty set of work, so for example
#EventQuery() would return #CUDA_SUCCESS.
flags include:
${ul(
"#EVENT_RECORD_DEFAULT: Default event creation flag.",
"""
#EVENT_RECORD_EXTERNAL: Event is captured in the graph as an external event node when performing stream capture. This flag is invalid outside
of stream capture.
"""
)}
""",
CUevent("hEvent", "event to record"),
nullable..CUstream("hStream", "stream to record event for"),
unsigned_int("flags", "see {@code CUevent_capture_flags}")
).ptsz()
CUresult(
"EventQuery",
"""
Queries an event's status.
Queries the status of all work currently captured by {@code hEvent}. See #EventRecord() for details on what is captured by an event.
Returns #CUDA_SUCCESS if all captured work has been completed, or #CUDA_ERROR_NOT_READY if any captured work is incomplete.
For the purposes of Unified Memory, a return value of #CUDA_SUCCESS is equivalent to having called #EventSynchronize().
""",
CUevent("hEvent", "event to query")
)
CUresult(
"EventSynchronize",
"""
Waits for an event to complete.
Waits until the completion of all work currently captured in {@code hEvent}. See #EventRecord() for details on what is captured by an event.
Waiting for an event that was created with the #EVENT_BLOCKING_SYNC flag will cause the calling CPU thread to block until the event has been
completed by the device. If the #EVENT_BLOCKING_SYNC flag has not been set, then the CPU thread will busy-wait until the event has been completed
by the device.
""",
CUevent("hEvent", "event to wait for")
)
IgnoreMissing..CUresult(
"EventDestroy",
"""
Destroys an event.
Destroys the event specified by {@code hEvent}.
An event may be destroyed before it is complete (i.e., while #EventQuery() would return #CUDA_ERROR_NOT_READY). In this case, the call does not
block on completion of the event, and any associated resources will automatically be released asynchronously at completion.
""",
CUevent("hEvent", "event to destroy")
).versioned()
CUresult(
"EventElapsedTime",
"""
Computes the elapsed time between two events.
Computes the elapsed time between two events (in milliseconds with a resolution of around 0.5 microseconds).
If either event was last recorded in a non-#NULL stream, the resulting time may be greater than expected (even if both used the same stream handle).
This happens because the #EventRecord() operation takes place asynchronously and there is no guarantee that the measured latency is actually just
between the two events. Any number of other different stream operations could execute in between the two measured events, thus altering the timing in a
significant way.
If #EventRecord() has not been called on either event then #CUDA_ERROR_INVALID_HANDLE is returned. If #EventRecord() has been called on both
events but one or both of them has not yet been completed (that is, #EventQuery() would return #CUDA_ERROR_NOT_READY on at least one of the
events), #CUDA_ERROR_NOT_READY is returned. If either event was created with the #EVENT_DISABLE_TIMING flag, then this function will return
#CUDA_ERROR_INVALID_HANDLE.
""",
Check(1)..float.p("pMilliseconds", "time between {@code hStart} and {@code hEnd} in ms"),
CUevent("hStart", "starting event"),
CUevent("hEnd", "ending event")
)
IgnoreMissing..CUresult(
"ImportExternalMemory",
"""
Imports an external memory object.
Imports an externally allocated memory object and returns a handle to that in {@code extMem_out}.
The properties of the handle being imported must be described in {@code memHandleDesc}.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD, then {@code ::handle::fd} must be a valid file descriptor referencing a memory object.
Ownership of the file descriptor is transferred to the CUDA driver when the handle is imported successfully. Performing any operations on the file
descriptor after it is imported results in undefined behavior.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32, then exactly one of {@code ::handle::win32::handle} and {@code ::handle::win32::name}
must not be #NULL. If {@code ::handle::win32::handle} is not #NULL, then it must represent a valid shared NT handle that references a memory object.
Ownership of this handle is not transferred to CUDA after the import operation, so the application must release the handle using the appropriate system
call. If {@code ::handle::win32::name} is not NULL, then it must point to a NULL-terminated array of UTF-16 characters that
refers to a memory object.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_WIN32_KMT, then {@code ::handle::win32::handle} must be non-#NULL and
{@code ::handle::win32::name} must be #NULL. The handle specified must be a globally shared KMT handle. This handle does not hold a reference to the
underlying object, and thus will be invalid when all references to the memory object are destroyed.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_HEAP, then exactly one of {@code ::handle::win32::handle} and {@code ::handle::win32::name}
must not be #NULL. If {@code ::handle::win32::handle} is not #NULL, then it must represent a valid shared NT handle that is returned by
{@code ID3D12Device::CreateSharedHandle} when referring to a {@code ID3D12Heap} object. This handle holds a reference to the underlying object. If
{@code ::handle::win32::name} is not #NULL, then it must point to a #NULL-terminated array of UTF-16 characters that refers to a {@code ID3D12Heap}
object.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE, then exactly one of {@code ::handle::win32::handle} and {@code ::handle::win32::name}
must not be NULL. If {@code ::handle::win32::handle} is not #NULL, then it must represent a valid shared NT handle that is returned by
{@code ID3D12Device::CreateSharedHandle} when referring to a {@code ID3D12Resource} object. This handle holds a reference to the underlying object. If
{@code ::handle::win32::name} is not #NULL, then it must point to a #NULL-terminated array of UTF-16 characters that refers to a {@code ID3D12Resource}
object.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE, then {@code ::handle::win32::handle} must represent a valid shared NT handle that is\
returned by {@code IDXGIResource1::CreateSharedHandle} when referring to a {@code ID3D11Resource} object. If {@code ::handle::win32::name} is not
#NULL, then it must point to a #NULL-terminated array of UTF-16 characters that refers to a {@code ID3D11Resource} object.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE_KMT, then {@code ::handle::win32::handle} must represent a valid shared KMT handle
that is returned by {@code IDXGIResource::GetSharedHandle} when referring to a {@code ID3D11Resource} object and {@code ::handle::win32::name} must be
#NULL.
If {@code ::type} is #EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF, then {@code ::handle::nvSciBufObject} must be non-#NULL and reference a valid
{@code NvSciBuf} object. If the {@code NvSciBuf} object imported into CUDA is also mapped by other drivers, then the application must use
#WaitExternalSemaphoresAsync() or #SignalExternalSemaphoresAsync() as appropriate barriers to maintain coherence between CUDA and the other drivers.
See #CUDA_EXTERNAL_SEMAPHORE_SIGNAL_SKIP_NVSCIBUF_MEMSYNC and #CUDA_EXTERNAL_SEMAPHORE_WAIT_SKIP_NVSCIBUF_MEMSYNC for memory synchronization.
The size of the memory object must be specified in {@code ::size}.
Specifying the flag #CUDA_EXTERNAL_MEMORY_DEDICATED in {@code ::flags} indicates that the resource is a dedicated resource. The definition of what a
dedicated resource is outside the scope of this extension. This flag must be set if {@code ::type} is one of the following:
${ul(
"#EXTERNAL_MEMORY_HANDLE_TYPE_D3D12_RESOURCE",
"#EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE",
"#EXTERNAL_MEMORY_HANDLE_TYPE_D3D11_RESOURCE_KMT"
)}
${note("""If the Vulkan memory imported into CUDA is mapped on the CPU then the application must use
{@code vkInvalidateMappedMemoryRanges}/{@code vkFlushMappedMemoryRanges} as well as appropriate Vulkan pipeline barriers to maintain coherence between
CPU and GPU. For more information on these APIs, please refer to "Synchronization and Cache Control" chapter from Vulkan specification.""")}
""",
Check(1)..CUexternalMemory.p("extMem_out", "returned handle to an external memory object"),
CUDA_EXTERNAL_MEMORY_HANDLE_DESC.const.p("memHandleDesc", "memory import handle descriptor")
)
IgnoreMissing..CUresult(
"ExternalMemoryGetMappedBuffer",
"""
Maps a buffer onto an imported memory object.
Maps a buffer onto an imported memory object and returns a device pointer in {@code devPtr}.
The properties of the buffer being mapped must be described in {@code bufferDesc}.
The offset and size have to be suitably aligned to match the requirements of the external API. Mapping two buffers whose ranges overlap may or may not
result in the same virtual address being returned for the overlapped portion. In such cases, the application must ensure that all accesses to that
region from the GPU are volatile. Otherwise writes made via one address are not guaranteed to be visible via the other address, even if they're issued
by the same thread. It is recommended that applications map the combined range instead of mapping separate buffers and then apply the appropriate
offsets to the returned pointer to derive the individual buffers.
The returned pointer {@code devPtr} must be freed using #MemFree().
""",
Check(1)..CUdeviceptr.p("devPtr", "returned device pointer to buffer"),
CUexternalMemory("extMem", "handle to external memory object"),
CUDA_EXTERNAL_MEMORY_BUFFER_DESC.const.p("bufferDesc", "buffer descriptor")
)
IgnoreMissing..CUresult(
"ExternalMemoryGetMappedMipmappedArray",
"""
Maps a CUDA mipmapped array onto an external memory object.
Maps a CUDA mipmapped array onto an external object and returns a handle to it in {@code mipmap}.
The properties of the CUDA mipmapped array being mapped must be described in {@code mipmapDesc}.
{@code ::offset} is the offset in the memory object where the base level of the mipmap chain is. {@code ::arrayDesc} describes the format, dimensions
and type of the base level of the mipmap chain. For further details on these parameters, please refer to the documentation for #MipmappedArrayCreate().
Note that if the mipmapped array is bound as a color target in the graphics API, then the flag #CUDA_ARRAY3D_COLOR_ATTACHMENT must be specified in
{@code ::arrayDesc::Flags}. {@code ::numLevels} specifies the total number of levels in the mipmap chain.
If {@code extMem} was imported from a handle of type #EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF, then {@code ::numLevels} must be equal to 1.
The returned CUDA mipmapped array must be freed using #MipmappedArrayDestroy().
""",
Check(1)..CUmipmappedArray.p("mipmap", "returned CUDA mipmapped array"),
CUexternalMemory("extMem", "handle to external memory object"),
CUDA_EXTERNAL_MEMORY_MIPMAPPED_ARRAY_DESC.const.p("mipmapDesc", "CUDA array descriptor")
)
IgnoreMissing..CUresult(
"DestroyExternalMemory",
"""
Destroys an external memory object.
Destroys the specified external memory object. Any existing buffers and CUDA mipmapped arrays mapped onto this object must no longer be used and must
be explicitly freed using #MemFree() and #MipmappedArrayDestroy() respectively.
""",
CUexternalMemory("extMem", "external memory object to be destroyed")
)
IgnoreMissing..CUresult(
"ImportExternalSemaphore",
"""
Imports an external semaphore.
Imports an externally allocated synchronization object and returns a handle to that in {@code extSem_out}.
The properties of the handle being imported must be described in {@code semHandleDesc}.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD, then {@code ::handle::fd} must be a valid file descriptor referencing a synchronization
object. Ownership of the file descriptor is transferred to the CUDA driver when the handle is imported successfully. Performing any operations on the
file descriptor after it is imported results in undefined behavior.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32, then exactly one of {@code ::handle::win32::handle} and
{@code ::handle::win32::name} must not be #NULL. If {@code ::handle::win32::handle} is not #NULL, then it must represent a valid shared NT handle that
references a synchronization object. Ownership of this handle is not transferred to CUDA after the import operation, so the application must release
the handle using the appropriate system call. If {@code ::handle::win32::name} is not #NULL, then it must name a valid synchronization object.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT, then {@code ::handle::win32::handle} must be non-NULL and
{@code ::handle::win32::name} must be #NULL. The handle specified must be a globally shared KMT handle. This handle does not hold a reference to the
underlying object, and thus will be invalid when all references to the synchronization object are destroyed.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE, then exactly one of {@code ::handle::win32::handle} and {@code ::handle::win32::name}
must not be #NULL. If {@code ::handle::win32::handle} is not #NULL, then it must represent a valid shared NT handle that is returned by
{@code ID3D12Device::CreateSharedHandle} when referring to a {@code ID3D12Fence} object. This handle holds a reference to the underlying object. If
{@code ::handle::win32::name} is not #NULL, then it must name a valid synchronization object that refers to a valid {@code ID3D12Fence} object.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_FENCE, then {@code ::handle::win32::handle} represents a valid shared NT handle that is
returned by {@code ID3D11Fence::CreateSharedHandle}. If {@code ::handle::win32::name} is not #NULL, then it must name a valid synchronization object
that refers to a valid {@code ID3D11Fence} object.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC, then {@code ::handle::nvSciSyncObj} represents a valid {@code NvSciSyncObj}.
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX, then {@code ::handle::win32::handle} represents a valid shared NT handle that is returned by
{@code IDXGIResource1::CreateSharedHandle} when referring to a {@code IDXGIKeyedMutex} object. If {@code ::handle::win32::name} is not #NULL, then it
must name a valid synchronization object that refers to a valid {@code IDXGIKeyedMutex} object.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX_KMT, then {@code ::handle::win32::handle} represents a valid shared KMT handle
that is returned by {@code IDXGIResource::GetSharedHandle} when referring to a {@code IDXGIKeyedMutex} object and {@code ::handle::win32::name} must be
#NULL.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD, then {@code ::handle::fd} must be a valid file descriptor referencing a
synchronization object. Ownership of the file descriptor is transferred to the CUDA driver when the handle is imported successfully. Performing any
operations on the file descriptor after it is imported results in undefined behavior.
If {@code ::type} is #EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_WIN32, then exactly one of {@code ::handle::win32::handle} and
{@code ::handle::win32::name} must not be #NULL. If {@code ::handle::win32::handle} is not #NULL, then it must represent a valid shared NT handle that
references a synchronization object. Ownership of this handle is not transferred to CUDA after the import operation, so the application must release
the handle using the appropriate system call. If {@code ::handle::win32::name} is not #NULL, then it must name a valid synchronization object.
""",
Check(1)..CUexternalSemaphore.p("extSem_out", "returned handle to an external semaphore"),
CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC.const.p("semHandleDesc", "semaphore import handle descriptor")
)
IgnoreMissing..CUresult(
"SignalExternalSemaphoresAsync",
"""
Signals a set of external semaphore objects,
Enqueues a signal operation on a set of externally allocated semaphore object in the specified stream. The operations will be executed when all prior
operations in the stream complete.
The exact semantics of signaling a semaphore depends on the type of the object.
If the semaphore object is any one of the following types: #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32, #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT then signaling the semaphore will set it to the
signaled state.
If the semaphore object is any one of the following types: #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_FENCE, #EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_WIN32 then the semaphore will be set to the value specified in {@code ::params::fence::value}.
If the semaphore object is of the type #EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC this API sets
{@code ::params::nvSciSync::fence} to a value that can be used by subsequent waiters of the same {@code NvSciSync} object to order operations with
those currently submitted in {@code stream}. Such an update will overwrite previous contents of {@code ::params::nvSciSync::fence}. By default,
signaling such an external semaphore object causes appropriate memory synchronization operations to be performed over all external memory objects that
are imported as #EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF. This ensures that any subsequent accesses made by other importers of the same set of NvSciBuf
memory object(s) are coherent. These operations can be skipped by specifying the flag #CUDA_EXTERNAL_SEMAPHORE_SIGNAL_SKIP_NVSCIBUF_MEMSYNC, which can
be used as a performance optimization when data coherency is not required. But specifying this flag in scenarios where data coherency is required
results in undefined behavior. Also, for semaphore object of the type #EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC, if the {@code NvSciSyncAttrList} used
to create the {@code NvSciSyncObj} had not set the flags in #DeviceGetNvSciSyncAttributes() to #CUDA_NVSCISYNC_ATTR_SIGNAL, this API will return
#CUDA_ERROR_NOT_SUPPORTED.
If the semaphore object is any one of the following types: #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX_KMT then the keyed mutex will be released with the key specified in {@code ::params::keyedmutex::key}.
""",
CUexternalSemaphore.const.p("extSemArray", "set of external semaphores to be signaled"),
CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS.const.p("paramsArray", "array of semaphore parameters"),
AutoSize("extSemArray", "paramsArray")..unsigned_int("numExtSems", "number of semaphores to signal"),
nullable..CUstream("stream", "stream to enqueue the signal operations in")
).ptsz()
IgnoreMissing..CUresult(
"WaitExternalSemaphoresAsync",
"""
Waits on a set of external semaphore objects.
Enqueues a wait operation on a set of externally allocated semaphore object in the specified stream. The operations will be executed when all prior
operations in the stream complete.
The exact semantics of waiting on a semaphore depends on the type of the object.
If the semaphore object is any one of the following types: #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32, #EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT then waiting on the semaphore will wait until
the semaphore reaches the signaled state. The semaphore will then be reset to the unsignaled state. Therefore for every signal operation, there can
only be one wait operation.
If the semaphore object is any one of the following types: #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_FENCE, #EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_WIN32 then waiting on the semaphore will wait until the value of the semaphore is greater than
or equal to {@code ::params::fence::value}.
If the semaphore object is of the type #EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC then, waiting on the semaphore will wait until the
{@code ::params::nvSciSync::fence} is signaled by the signaler of the NvSciSyncObj that was associated with this semaphore object. By default, waiting
on such an external semaphore object causes appropriate memory synchronization operations to be performed over all external memory objects that are
imported as #EXTERNAL_MEMORY_HANDLE_TYPE_NVSCIBUF. This ensures that any subsequent accesses made by other importers of the same set of
{@code NvSciBuf} memory object(s) are coherent. These operations can be skipped by specifying the flag
#CUDA_EXTERNAL_SEMAPHORE_WAIT_SKIP_NVSCIBUF_MEMSYNC, which can be used as a performance optimization when data coherency is not required. But
specifying this flag in scenarios where data coherency is required results in undefined behavior. Also, for semaphore object of the type
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC, if the {@code NvSciSyncAttrList} used to create the {@code NvSciSyncObj} had not set the flags in
#DeviceGetNvSciSyncAttributes() to #CUDA_NVSCISYNC_ATTR_WAIT, this API will return #CUDA_ERROR_NOT_SUPPORTED.
If the semaphore object is any one of the following types: #EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX,
#EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX_KMT then the keyed mutex will be acquired when it is released with the key specified in
{@code ::params::keyedmutex::key} or until the timeout specified by {@code ::params::keyedmutex::timeoutMs} has lapsed. The timeout interval can either
be a finite value specified in milliseconds or an infinite value. In case an infinite value is specified the timeout never elapses. The windows
{@code INFINITE} macro must be used to specify infinite timeout.
""",
CUexternalSemaphore.const.p("extSemArray", "external semaphores to be waited on"),
CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS.const.p("paramsArray", "array of semaphore parameters"),
AutoSize("extSemArray", "paramsArray")..unsigned_int("numExtSems", "number of semaphores to wait on"),
nullable..CUstream("stream", "stream to enqueue the wait operations in")
).ptsz()
IgnoreMissing..CUresult(
"DestroyExternalSemaphore",
"""
Destroys an external semaphore.
Destroys an external semaphore object and releases any references to the underlying resource. Any outstanding signals or waits must have completed
before the semaphore is destroyed.
""",
CUexternalSemaphore("extSem", "external semaphore to be destroyed")
)
IgnoreMissing..CUresult(
"StreamWaitValue32",
"""
Wait on a memory location.
Enqueues a synchronization of the stream on the given memory location. Work ordered after the operation will block until the given condition on the
memory is satisfied. By default, the condition is to wait for {@code (int32_t)(*addr - value) >= 0}, a cyclic greater-or-equal. Other condition types
can be specified via {@code flags}.
If the memory was registered via #MemHostRegister(), the device pointer should be obtained with #MemHostGetDevicePointer(). This function cannot be
used with managed memory (#MemAllocManaged()).
Support for this can be queried with #DeviceGetAttribute() and #DEVICE_ATTRIBUTE_CAN_USE_STREAM_MEM_OPS.
Support for #STREAM_WAIT_VALUE_NOR can be queried with #DeviceGetAttribute() and #DEVICE_ATTRIBUTE_CAN_USE_STREAM_WAIT_VALUE_NOR.
""",
nullable..CUstream("stream", "the stream to synchronize on the memory location"),
CUdeviceptr("addr", "the memory location to wait on"),
cuuint32_t("value", "the value to compare with the memory location"),
unsigned_int("flags", "see {@code CUstreamWaitValue_flags}")
).ptsz()
IgnoreMissing..CUresult(
"StreamWaitValue64",
"""
Wait on a memory location.
Enqueues a synchronization of the stream on the given memory location. Work ordered after the operation will block until the given condition on the
memory is satisfied. By default, the condition is to wait for {@code (int64_t)(*addr - value) >= 0}, a cyclic greater-or-equal. Other condition types
can be specified via {@code flags}.
If the memory was registered via #MemHostRegister(), the device pointer should be obtained with #MemHostGetDevicePointer().
Support for this can be queried with #DeviceGetAttribute() and #DEVICE_ATTRIBUTE_CAN_USE_64_BIT_STREAM_MEM_OPS.
""",
nullable..CUstream("stream", "the stream to synchronize on the memory location"),
CUdeviceptr("addr", "the memory location to wait on"),
cuuint64_t("value", "the value to compare with the memory location"),
unsigned_int("flags", "see {@code CUstreamWaitValue_flags}")
).ptsz()
IgnoreMissing..CUresult(
"StreamWriteValue32",
"""
Write a value to memory.
Write a value to memory. Unless the #STREAM_WRITE_VALUE_NO_MEMORY_BARRIER flag is passed, the write is preceded by a system-wide memory fence,
equivalent to a {@code __threadfence_system()} but scoped to the stream rather than a CUDA thread.
If the memory was registered via #MemHostRegister(), the device pointer should be obtained with #MemHostGetDevicePointer(). This function cannot
be used with managed memory (#MemAllocManaged()).
Support for this can be queried with #DeviceGetAttribute() and #DEVICE_ATTRIBUTE_CAN_USE_STREAM_MEM_OPS.
""",
nullable..CUstream("stream", "the stream to do the write in"),
CUdeviceptr("addr", "the device address to write to"),
cuuint32_t("value", "the value to write"),
unsigned_int("flags", "see {@code CUstreamWriteValue_flags}")
).ptsz()
IgnoreMissing..CUresult(
"StreamWriteValue64",
"""
Write a value to memory.
Write a value to memory. Unless the #STREAM_WRITE_VALUE_NO_MEMORY_BARRIER flag is passed, the write is preceded by a system-wide memory fence,
equivalent to a {@code __threadfence_system()} but scoped to the stream rather than a CUDA thread.
If the memory was registered via #MemHostRegister(), the device pointer should be obtained with #MemHostGetDevicePointer().
Support for this can be queried with #DeviceGetAttribute() and #DEVICE_ATTRIBUTE_CAN_USE_64_BIT_STREAM_MEM_OPS.
""",
nullable..CUstream("stream", "the stream to do the write in"),
CUdeviceptr("addr", "the device address to write to"),
cuuint64_t("value", "the value to write"),
unsigned_int("flags", "see {@code CUstreamWriteValue_flags}")
)
IgnoreMissing..CUresult(
"StreamBatchMemOp",
"""
Batch operations to synchronize the stream via memory operations.
This is a batch version of #StreamWaitValue32() and #StreamWriteValue32(). Batching operations may avoid some performance overhead in both the
API call and the device execution versus adding them to the stream in separate API calls. The operations are enqueued in the order they appear in the
array.
See {@code CUstreamBatchMemOpType} for the full set of supported operations, and #StreamWaitValue32(), #StreamWaitValue64(), #StreamWriteValue32(),
and #StreamWriteValue64() for details of specific operations.
Basic support for this can be queried with #DeviceGetAttribute() and #DEVICE_ATTRIBUTE_CAN_USE_STREAM_MEM_OPS. See related APIs for details on
querying support for specific operations.
""",
nullable..CUstream("stream", "the stream to enqueue the operations in"),
AutoSize("paramArray")..unsigned_int("count", "the number of operations in the array. Must be less than 256."),
CUstreamBatchMemOpParams.p("paramArray", "the types and parameters of the individual operations"),
unsigned_int("flags", "reserved for future expansion; must be 0")
).ptsz()
CUresult(
"FuncGetAttribute",
"""
Returns information about a function.
Returns in {@code *pi} the integer value of the attribute {@code attrib} on the kernel given by {@code hfunc}. The supported attributes are:
${ul(
"""
#FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK: The maximum number of threads per block, beyond which a launch of the function would fail. This number
depends on both the function and the device on which the function is currently loaded.
""",
"""
#FUNC_ATTRIBUTE_SHARED_SIZE_BYTES: The size in bytes of statically-allocated shared memory per block required by this function. This does not
include dynamically-allocated shared memory requested by the user at runtime.
""",
"#FUNC_ATTRIBUTE_CONST_SIZE_BYTES: The size in bytes of user-allocated constant memory required by this function.",
"#FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES: The size in bytes of local memory used by each thread of this function.",
"#FUNC_ATTRIBUTE_NUM_REGS: The number of registers used by each thread of this function.",
"""
#FUNC_ATTRIBUTE_PTX_VERSION: The PTX virtual architecture version for which the function was compiled. This value is the major PTX version * 10
+ the minor PTX version, so a PTX version 1.3 function would return the value 13. Note that this may return the undefined value of 0 for cubins
compiled prior to CUDA 3.0.
""",
"""
#FUNC_ATTRIBUTE_BINARY_VERSION: The binary architecture version for which the function was compiled. This value is the major binary version *
10 + the minor binary version, so a binary version 1.3 function would return the value 13. Note that this will return a value of 10 for legacy
cubins that do not have a properly-encoded binary architecture version.
""",
"""
#FUNC_ATTRIBUTE_CACHE_MODE_CA: The attribute to indicate whether the function has been compiled with user specified option "-Xptxas --dlcm=ca"
set.
""",
"#FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES: The maximum size in bytes of dynamically-allocated shared memory.",
"#FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT: Preferred shared memory-L1 cache split ratio in percent of total shared memory."
)}
""",
Check(1)..int.p("pi", "returned attribute value"),
CUfunction_attribute("attrib", "attribute requested"),
CUfunction("hfunc", "function to query attribute of")
)
IgnoreMissing..CUresult(
"FuncSetAttribute",
"""
Sets information about a function.
This call sets the value of a specified attribute {@code attrib} on the kernel given by {@code hfunc} to an integer value specified by {@code val} This
function returns #CUDA_SUCCESS if the new value of the attribute could be successfully set. If the set fails, this call will return an error. Not all
attributes can have values set. Attempting to set a value on a read-only attribute will result in an error (#CUDA_ERROR_INVALID_VALUE).
Supported attributes for the cuFuncSetAttribute call are:
${ul(
"""
#FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES: This maximum size in bytes of dynamically-allocated shared memory. The value should contain the
requested maximum size of dynamically-allocated shared memory. The sum of this value and the function attribute
#FUNC_ATTRIBUTE_SHARED_SIZE_BYTES cannot exceed the device attribute #DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_BLOCK_OPTIN. The maximal size
of requestable dynamic shared memory may differ by GPU architecture.
""",
"""
#FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT: On devices where the L1 cache and shared memory use the same hardware resources, this sets
the shared memory carveout preference, in percent of the total shared memory. See #DEVICE_ATTRIBUTE_MAX_SHARED_MEMORY_PER_MULTIPROCESSOR This
is only a hint, and the driver can choose a different ratio if required to execute the function.
"""
)}
""",
CUfunction("hfunc", "function to query attribute of"),
CUfunction_attribute("attrib", "attribute requested"),
int("value", "the value to set")
)
CUresult(
"FuncSetCacheConfig",
"""
Sets the preferred cache configuration for a device function.
On devices where the L1 cache and shared memory use the same hardware resources, this sets through {@code config} the preferred cache configuration for
the device function {@code hfunc}. This is only a preference. The driver will use the requested configuration if possible, but it is free to choose a
different configuration if required to execute {@code hfunc}. Any context-wide preference set via #CtxSetCacheConfig() will be overridden by this
per-function setting unless the per-function setting is #FUNC_CACHE_PREFER_NONE. In that case, the current context-wide setting will be used.
This setting does nothing on devices where the size of the L1 cache and shared memory are fixed.
Launching a kernel with a different preference than the most recent preference setting may insert a device-side synchronization point.
The supported cache configurations are:
${ul(
"#FUNC_CACHE_PREFER_NONE: no preference for shared memory or L1 (default)",
"#FUNC_CACHE_PREFER_SHARED: prefer larger shared memory and smaller L1 cache",
"#FUNC_CACHE_PREFER_L1: prefer larger L1 cache and smaller shared memory",
"#FUNC_CACHE_PREFER_EQUAL: prefer equal sized L1 cache and shared memory"
)}
""",
CUfunction("hfunc", "kernel to configure cache for"),
CUfunc_cache("config", "requested cache configuration")
)
IgnoreMissing..CUresult(
"FuncSetSharedMemConfig",
"""
Sets the shared memory configuration for a device function.
On devices with configurable shared memory banks, this function will force all subsequent launches of the specified device function to have the given
shared memory bank size configuration. On any given launch of the function, the shared memory configuration of the device will be temporarily changed
if needed to suit the function's preferred configuration. Changes in shared memory configuration between subsequent launches of functions, may
introduce a device side synchronization point.
Any per-function setting of shared memory bank size set via #FuncSetSharedMemConfig() will override the context wide setting set with
#CtxSetSharedMemConfig().
Changing the shared memory bank size will not increase shared memory usage or affect occupancy of kernels, but may have major effects on performance.
Larger bank sizes will allow for greater potential bandwidth to shared memory, but will change what kinds of accesses to shared memory will result in
bank conflicts.
This function will do nothing on devices with fixed shared memory bank size.
The supported bank configurations are:
${ul(
"#SHARED_MEM_CONFIG_DEFAULT_BANK_SIZE: use the context's shared memory configuration when launching this function.",
"#SHARED_MEM_CONFIG_FOUR_BYTE_BANK_SIZE: set shared memory bank width to be natively four bytes when launching this function.",
"#SHARED_MEM_CONFIG_EIGHT_BYTE_BANK_SIZE: set shared memory bank width to be natively eight bytes when launching this function."
)}
""",
CUfunction("hfunc", "kernel to be given a shared memory config"),
CUsharedconfig("config", "requested shared memory configuration")
)
IgnoreMissing..CUresult(
"FuncGetModule",
"""
Returns a module handle.
Returns in {@code *hmod} the handle of the module that function {@code hfunc} is located in. The lifetime of the module corresponds to the lifetime of
the context it was loaded in or until the module is explicitly unloaded.
The CUDA runtime manages its own modules loaded into the primary context. If the handle returned by this API refers to a module loaded by the CUDA
runtime, calling #ModuleUnload() on that module will result in undefined behavior.
""",
Check(1)..CUmodule.p("hmod", "returned module handle"),
CUfunction("hfunc", "function to retrieve module for")
)
IgnoreMissing..CUresult(
"LaunchKernel",
"""
Launches a CUDA function.
Invokes the kernel {@code f} on a {@code gridDimX} x {@code gridDimY} x {@code gridDimZ} grid of blocks. Each block contains {@code blockDimX} x {@code
blockDimY} x {@code blockDimZ} threads.
{@code sharedMemBytes} sets the amount of dynamic shared memory that will be available to each thread block.
Kernel parameters to {@code f} can be specified in one of two ways:
${ol(
"""
Kernel parameters can be specified via {@code kernelParams}.
If {@code f} has N parameters, then {@code kernelParams} needs to be an array of N pointers. Each of {@code kernelParams[0]} through
{@code kernelParams[N-1]} must point to a region of memory from which the actual kernel parameter will be copied. The number of kernel parameters
and their offsets and sizes do not need to be specified as that information is retrieved directly from the kernel's image.
""",
"""
Kernel parameters can also be packaged by the application into a single buffer that is passed in via the {@code extra} parameter.
This places the burden on the application of knowing each kernel parameter's size and alignment/padding within the buffer. Here is an example of
using the {@code extra} parameter in this manner:
${codeBlock("""
size_t argBufferSize;
char argBuffer[256];
// populate argBuffer and argBufferSize
void *config[] = {
CU_LAUNCH_PARAM_BUFFER_POINTER, argBuffer,
CU_LAUNCH_PARAM_BUFFER_SIZE, &argBufferSize,
CU_LAUNCH_PARAM_END
};
status = cuLaunchKernel(f, gx, gy, gz, bx, by, bz, sh, s, NULL, config);""")}
"""
)}
The {@code extra} parameter exists to allow {@code cuLaunchKernel()} to take additional less commonly used arguments. {@code extra} specifies a list of
names of extra settings and their corresponding values. Each extra setting name is immediately followed by the corresponding value. The list must be
terminated with either #NULL or #LAUNCH_PARAM_END.
${ul(
"#LAUNCH_PARAM_END, which indicates the end of the {@code extra} array",
"""
#LAUNCH_PARAM_BUFFER_POINTER, which specifies that the next value in {@code extra} will be a pointer to a buffer containing all the kernel
parameters for launching kernel {@code f}
""",
"""
#LAUNCH_PARAM_BUFFER_SIZE, which specifies that the next value in {@code extra} will be a pointer to a size_t containing the size of the buffer
specified with #LAUNCH_PARAM_BUFFER_POINTER
"""
)}
The error #CUDA_ERROR_INVALID_VALUE will be returned if kernel parameters are specified with both {@code kernelParams} and {@code extra} (i.e. both
{@code kernelParams} and {@code extra} are non-#NULL).
Calling {@code cuLaunchKernel()} invalidates the persistent function state set through the following deprecated APIs: #FuncSetBlockShape(),
#FuncSetSharedSize(), #ParamSetSize(), #ParamSeti(), #ParamSetf(), #ParamSetv().
Note that to use #LaunchKernel(), the kernel {@code f} must either have been compiled with toolchain version 3.2 or later so that it will contain
kernel parameter information, or have no kernel parameters. If either of these conditions is not met, then #LaunchKernel() will return
#CUDA_ERROR_INVALID_IMAGE.
""",
CUfunction("f", "kernel to launch"),
unsigned_int("gridDimX", "width of grid in blocks"),
unsigned_int("gridDimY", "height of grid in blocks"),
unsigned_int("gridDimZ", "depth of grid in blocks"),
unsigned_int("blockDimX", "x dimension of each thread block"),
unsigned_int("blockDimY", "y dimension of each thread block"),
unsigned_int("blockDimZ", "z dimension of each thread block"),
unsigned_int("sharedMemBytes", "dynamic shared-memory size per thread block in bytes"),
nullable..CUstream("hStream", "stream identifier"),
Unsafe..nullable..void.p.p("kernelParams", "array of pointers to kernel parameters"),
Unsafe..nullable..void.p.p("extra", "extra options")
).ptsz()
IgnoreMissing..CUresult(
"LaunchCooperativeKernel",
"""
Launches a CUDA function where thread blocks can cooperate and synchronize as they execute.
Invokes the kernel {@code f} on a {@code gridDimX} x {@code gridDimY} x {@code gridDimZ} grid of blocks. Each block contains {@code blockDimX} x {@code
blockDimY} x {@code blockDimZ} threads.
{@code sharedMemBytes} sets the amount of dynamic shared memory that will be available to each thread block.
The device on which this kernel is invoked must have a non-zero value for the device attribute #DEVICE_ATTRIBUTE_COOPERATIVE_LAUNCH.
The total number of blocks launched cannot exceed the maximum number of blocks per multiprocessor as returned by
#OccupancyMaxActiveBlocksPerMultiprocessor() (or #OccupancyMaxActiveBlocksPerMultiprocessorWithFlags()) times the number of multiprocessors as
specified by the device attribute #DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT.
The kernel cannot make use of CUDA dynamic parallelism.
Kernel parameters must be specified via {@code kernelParams}. If {@code f} has N parameters, then {@code kernelParams} needs to be an array of N
pointers. Each of {@code kernelParams[0]} through {@code kernelParams[N-1]} must point to a region of memory from which the actual kernel parameter
will be copied. The number of kernel parameters and their offsets and sizes do not need to be specified as that information is retrieved directly from
the kernel's image.
Calling #LaunchCooperativeKernel() sets persistent function state that is the same as function state set through #LaunchKernel() API
When the kernel {@code f} is launched via #LaunchCooperativeKernel(), the previous block shape, shared size and parameter info associated with
{@code f} is overwritten.
Note that to use #LaunchCooperativeKernel(), the kernel {@code f} must either have been compiled with toolchain version 3.2 or later so that it will
contain kernel parameter information, or have no kernel parameters. If either of these conditions is not met, then #LaunchCooperativeKernel() will
return #CUDA_ERROR_INVALID_IMAGE.
""",
CUfunction("f", "kernel to launch"),
unsigned_int("gridDimX", "width of grid in blocks"),
unsigned_int("gridDimY", "height of grid in blocks"),
unsigned_int("gridDimZ", "depth of grid in blocks"),
unsigned_int("blockDimX", "x dimension of each thread block"),
unsigned_int("blockDimY", "y dimension of each thread block"),
unsigned_int("blockDimZ", "z dimension of each thread block"),
unsigned_int("sharedMemBytes", "dynamic shared-memory size per thread block in bytes"),
nullable..CUstream("hStream", "stream identifier"),
Unsafe..nullable..void.p.p("kernelParams", "array of pointers to kernel parameters")
).ptsz()
IgnoreMissing..CUresult(
"LaunchCooperativeKernelMultiDevice",
"""
Launches CUDA functions on multiple devices where thread blocks can cooperate and synchronize as they executeDeprecated: This function is deprecated as
of CUDA 11.3.
Invokes kernels as specified in the {@code launchParamsList} array where each element of the array specifies all the parameters required to perform a
single kernel launch. These kernels can cooperate and synchronize as they execute. The size of the array is specified by {@code numDevices}.
No two kernels can be launched on the same device. All the devices targeted by this multi-device launch must be identical. All devices must have a
non-zero value for the device attribute #DEVICE_ATTRIBUTE_COOPERATIVE_MULTI_DEVICE_LAUNCH.
All kernels launched must be identical with respect to the compiled code. Note that any __device__ __constant__ or __managed__ variables present
in the module that owns the kernel launched on each device, are independently instantiated on every device. It is the application's responsibility to
ensure these variables are initialized and used appropriately.
The size of the grids as specified in blocks, the size of the blocks themselves and the amount of shared memory used by each thread block must also
match across all launched kernels.
The streams used to launch these kernels must have been created via either #StreamCreate() or #StreamCreateWithPriority(). The #NULL stream or
#STREAM_LEGACY or #STREAM_PER_THREAD cannot be used.
The total number of blocks launched per kernel cannot exceed the maximum number of blocks per multiprocessor as returned by
#OccupancyMaxActiveBlocksPerMultiprocessor() (or #OccupancyMaxActiveBlocksPerMultiprocessorWithFlags()) times the number of multiprocessors as
specified by the device attribute #DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT. Since the total number of blocks launched per device has to match across
all devices, the maximum number of blocks that can be launched per device will be limited by the device with the least number of multiprocessors.
The kernels cannot make use of CUDA dynamic parallelism.
${ul(
"{@code CUDA_LAUNCH_PARAMS::function} specifies the kernel to be launched. All functions must be identical with respect to the compiled code.",
"{@code CUDA_LAUNCH_PARAMS::gridDimX} is the width of the grid in blocks. This must match across all kernels launched.",
"{@code CUDA_LAUNCH_PARAMS::gridDimY} is the height of the grid in blocks. This must match across all kernels launched.",
"{@code CUDA_LAUNCH_PARAMS::gridDimZ} is the depth of the grid in blocks. This must match across all kernels launched.",
"{@code CUDA_LAUNCH_PARAMS::blockDimX} is the X dimension of each thread block. This must match across all kernels launched.",
"{@code CUDA_LAUNCH_PARAMS::blockDimX} is the Y dimension of each thread block. This must match across all kernels launched.",
"{@code CUDA_LAUNCH_PARAMS::blockDimZ} is the Z dimension of each thread block. This must match across all kernels launched.",
"""
{@code CUDA_LAUNCH_PARAMS::sharedMemBytes} is the dynamic shared-memory size per thread block in bytes. This must match across all kernels
launched.
""",
"""
{@code CUDA_LAUNCH_PARAMS::hStream} is the handle to the stream to perform the launch in. This cannot be the #NULL stream or #STREAM_LEGACY or
#STREAM_PER_THREAD. The CUDA context associated with this stream must match that associated with {@code CUDA_LAUNCH_PARAMS::function}.
""",
"""
{@code CUDA_LAUNCH_PARAMS::kernelParams} is an array of pointers to kernel parameters. If {@code ::function} has N parameters, then
{@code ::kernelParams} needs to be an array of N pointers. Each of {@code ::kernelParams[0]} through {@code ::kernelParams[N-1]} must point to a
region of memory from which the actual kernel parameter will be copied. The number of kernel parameters and their offsets and sizes do not need to
be specified as that information is retrieved directly from the kernel's image.
"""
)}
By default, the kernel won't begin execution on any GPU until all prior work in all the specified streams has completed. This behavior can be
overridden by specifying the flag #CUDA_COOPERATIVE_LAUNCH_MULTI_DEVICE_NO_PRE_LAUNCH_SYNC. When this flag is specified, each kernel will only wait
for prior work in the stream corresponding to that GPU to complete before it begins execution.
Similarly, by default, any subsequent work pushed in any of the specified streams will not begin execution until the kernels on all GPUs have
completed. This behavior can be overridden by specifying the flag #CUDA_COOPERATIVE_LAUNCH_MULTI_DEVICE_NO_POST_LAUNCH_SYNC. When this flag is
specified, any subsequent work pushed in any of the specified streams will only wait for the kernel launched on the GPU corresponding to that stream to
complete before it begins execution.
Calling #LaunchCooperativeKernelMultiDevice() sets persistent function state that is the same as function state set through #LaunchKernel() API
when called individually for each element in {@code launchParamsList}.
When kernels are launched via #LaunchCooperativeKernelMultiDevice(), the previous block shape, shared size and parameter info associated with each
{@code CUDA_LAUNCH_PARAMS::function} in {@code launchParamsList} is overwritten.
Note that to use #LaunchCooperativeKernelMultiDevice(), the kernels must either have been compiled with toolchain version 3.2 or later so that it
will contain kernel parameter information, or have no kernel parameters. If either of these conditions is not met, then
#LaunchCooperativeKernelMultiDevice() will return #CUDA_ERROR_INVALID_IMAGE.
""",
CUDA_LAUNCH_PARAMS.p("launchParamsList", "list of launch parameters, one per device"),
AutoSize("launchParamsList")..unsigned_int("numDevices", "size of the {@code launchParamsList} array"),
unsigned_int("flags", "flags to control launch behavior")
)
IgnoreMissing..CUresult(
"LaunchHostFunc",
"""
Enqueues a host function call in a stream.
Enqueues a host function to run in a stream. The function will be called after currently enqueued work and will block work added after it.
The host function must not make any CUDA API calls. Attempting to use a CUDA API may result in #CUDA_ERROR_NOT_PERMITTED, but this is not required.
The host function must not perform any synchronization that may depend on outstanding CUDA work not mandated to run earlier. Host functions without a
mandated order (such as in independent streams) execute in undefined order and may be serialized.
For the purposes of Unified Memory, execution makes a number of guarantees:
${ul(
"""
The stream is considered idle for the duration of the function's execution. Thus, for example, the function may always use memory attached to the
stream it was enqueued in.
""",
"""
The start of execution of the function has the same effect as synchronizing an event recorded in the same stream immediately prior to the function.
It thus synchronizes streams which have been "joined" prior to the function.
""",
"""
Adding device work to any stream does not have the effect of making the stream active until all preceding host functions and stream callbacks have
executed. Thus, for example, a function might use global attached memory even if work has been added to another stream, if the work has been
ordered behind the function call with an event.
""",
"""
Completion of the function does not cause a stream to become active except as described above. The stream will remain idle if no device work
follows the function, and will remain idle across consecutive host functions or stream callbacks without device work in between. Thus, for example,
stream synchronization can be done by signaling from a host function at the end of the stream.
"""
)}
Note that, in contrast to #StreamAddCallback(), the function will not be called in the event of an error in the CUDA context.
""",
nullable..CUstream("hStream", "stream to enqueue function call in"),
CUhostFn("fn", "the function to call once preceding stream operations are complete"),
opaque_p("userData", "user-specified data to be passed to the function")
).ptsz()
CUresult(
"FuncSetBlockShape",
"""
Sets the block-dimensions for the function. (Deprecated)
Specifies the {@code x}, {@code y}, and {@code z} dimensions of the thread blocks that are created when the kernel given by {@code hfunc} is launched.
""",
CUfunction("hfunc", "kernel to specify dimensions of"),
int("x", "x dimension"),
int("y", "y dimension"),
int("z", "z dimension")
)
CUresult(
"FuncSetSharedSize",
"""
Sets the dynamic shared-memory size for the function. (Deprecated)
Sets through {@code bytes} the amount of dynamic shared memory that will be available to each thread block when the kernel given by {@code hfunc} is
launched.
""",
CUfunction("hfunc", "kernel to specify dynamic shared-memory size for"),
unsigned_int("bytes", "dynamic shared-memory size per thread in bytes")
)
CUresult(
"ParamSetSize",
"""
Sets the parameter size for the function. (Deprecated)
Sets through {@code numbytes} the total size in bytes needed by the function parameters of the kernel corresponding to {@code hfunc}.
""",
CUfunction("hfunc", "kernel to set parameter size for"),
unsigned_int("numbytes", "size of parameter list in bytes")
)
CUresult(
"ParamSeti",
"""
Adds an integer parameter to the function's argument listDeprecated:
Sets an integer parameter that will be specified the next time the kernel corresponding to {@code hfunc} will be invoked. {@code offset} is a byte
offset.
""",
CUfunction("hfunc", "kernel to add parameter to"),
int("offset", "offset to add parameter to argument list"),
unsigned_int("value", "value of parameter")
)
CUresult(
"ParamSetf",
"""
Adds a floating-point parameter to the function's argument list. (Deprecated)
Sets a floating-point parameter that will be specified the next time the kernel corresponding to {@code hfunc} will be invoked. {@code offset} is a
byte offset.
""",
CUfunction("hfunc", "kernel to add parameter to"),
int("offset", "offset to add parameter to argument list"),
float("value", "value of parameter")
)
CUresult(
"ParamSetv",
"""
Adds arbitrary data to the function's argument list. (Deprecated)
Copies an arbitrary amount of data (specified in {@code numbytes}) from {@code ptr} into the parameter space of the kernel corresponding to
{@code hfunc}. {@code offset} is a byte offset.
""",
CUfunction("hfunc", "kernel to add data to"),
int("offset", "offset to add data to argument list"),
void.p("ptr", "pointer to arbitrary data"),
AutoSize("ptr")..unsigned_int("numbytes", "size of data to copy in bytes")
)
CUresult(
"Launch",
"""
Launches a CUDA function. (Deprecated)
Invokes the kernel {@code f} on a 1 x 1 x 1 grid of blocks. The block contains the number of threads specified by a previous call to
#FuncSetBlockShape().
The block shape, dynamic shared memory size, and parameter information must be set using #FuncSetBlockShape(), #FuncSetSharedSize(),
#ParamSetSize(), #ParamSeti(), #ParamSetf(), and #ParamSetv() prior to calling this function.
Launching a function via #LaunchKernel() invalidates the function's block shape, dynamic shared memory size, and parameter information. After
launching via cuLaunchKernel, this state must be re-initialized prior to calling this function. Failure to do so results in undefined behavior.
""",
CUfunction("f", "kernel to launch")
)
CUresult(
"LaunchGrid",
"""
Launches a CUDA function. (Deprecated)
Invokes the kernel {@code f} on a {@code grid_width} x {@code grid_height} grid of blocks. Each block contains the number of threads specified by a
previous call to #FuncSetBlockShape().
The block shape, dynamic shared memory size, and parameter information must be set using #FuncSetBlockShape(), #FuncSetSharedSize(),
#ParamSetSize(), #ParamSeti(), #ParamSetf(), and #ParamSetv() prior to calling this function.
Launching a function via #LaunchKernel() invalidates the function's block shape, dynamic shared memory size, and parameter information. After
launching via cuLaunchKernel, this state must be re-initialized prior to calling this function. Failure to do so results in undefined behavior.
""",
CUfunction("f", "kernel to launch"),
int("grid_width", "width of grid in blocks"),
int("grid_height", "height of grid in blocks")
)
CUresult(
"LaunchGridAsync",
"""
Launches a CUDA function. (Deprecated)
Invokes the kernel {@code f} on a {@code grid_width} x {@code grid_height} grid of blocks. Each block contains the number of threads specified by a
previous call to #FuncSetBlockShape().
The block shape, dynamic shared memory size, and parameter information must be set using #FuncSetBlockShape(), #FuncSetSharedSize(),
#ParamSetSize(), #ParamSeti(), #ParamSetf(), and #ParamSetv() prior to calling this function.
Launching a function via #LaunchKernel() invalidates the function's block shape, dynamic shared memory size, and parameter information. After
launching via cuLaunchKernel, this state must be re-initialized prior to calling this function. Failure to do so results in undefined behavior.
${note("""In certain cases where cubins are created with no ABI (i.e., using {@code ptxas} {@code --abi-compile} {@code no}), this function may
serialize kernel launches. The CUDA driver retains asynchronous behavior by growing the per-thread stack as needed per launch and not shrinking it
afterwards.""")}
""",
CUfunction("f", "kernel to launch"),
int("grid_width", "width of grid in blocks"),
int("grid_height", "height of grid in blocks"),
nullable..CUstream("hStream", "stream identifier")
)
CUresult(
"ParamSetTexRef",
"""
Adds a texture-reference to the function's argument list. (Deprecated)
Makes the CUDA array or linear memory bound to the texture reference {@code hTexRef} available to a device program as a texture. In this version of
CUDA, the texture-reference must be obtained via #ModuleGetTexRef() and the {@code texunit} parameter must be set to #PARAM_TR_DEFAULT.
""",
CUfunction("hfunc", "kernel to add texture-reference to"),
int("texunit", "texture unit (must be #PARAM_TR_DEFAULT)"),
CUtexref("hTexRef", "texture-reference to add to argument list")
)
IgnoreMissing..CUresult(
"GraphCreate",
"""
Creates a graph.
Creates an empty graph, which is returned via {@code phGraph}.
""",
Check(1)..CUgraph.p("phGraph", "returns newly created graph"),
unsigned_int("flags", "graph creation flags, must be 0")
)
IgnoreMissing..CUresult(
"GraphAddKernelNode",
"""
Creates a kernel execution node and adds it to a graph.
Creates a new kernel execution node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies} and
arguments specified in {@code nodeParams}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the
graph. {@code dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
When the graph is launched, the node will invoke kernel {@code func} on a ({@code gridDimX} x {@code gridDimY} x {@code gridDimZ}) grid of blocks. Each
block contains ({@code blockDimX} x {@code blockDimY} x {@code blockDimZ}) threads.
{@code sharedMemBytes} sets the amount of dynamic shared memory that will be available to each thread block.
Kernel parameters to {@code func} can be specified in one of two ways:
${ol(
"""
Kernel parameters can be specified via {@code kernelParams}. If the kernel has N parameters, then {@code kernelParams} needs to be an array of N
pointers. Each pointer, from {@code kernelParams[0]} to {@code kernelParams[N-1]}, points to the region of memory from which the actual parameter
will be copied. The number of kernel parameters and their offsets and sizes do not need to be specified as that information is retrieved directly
from the kernel's image.
""",
"""
Kernel parameters for non-cooperative kernels can also be packaged by the application into a single buffer that is passed in via {@code extra}.
This places the burden on the application of knowing each kernel parameter's size and alignment/padding within the buffer. The {@code extra}
parameter exists to allow this function to take additional less commonly used arguments. {@code extra} specifies a list of names of extra settings
and their corresponding values. Each extra setting name is immediately followed by the corresponding value. The list must be terminated with either
#NULL or #LAUNCH_PARAM_END.
${ul(
"#LAUNCH_PARAM_END, which indicates the end of the {@code extra} array;",
"""
#LAUNCH_PARAM_BUFFER_POINTER, which specifies that the next value in {@code extra} will be a pointer to a buffer containing all the kernel
parameters for launching kernel {@code func;}
""",
"""
#LAUNCH_PARAM_BUFFER_SIZE, which specifies that the next value in {@code extra} will be a pointer to a size_t containing the size of the buffer
specified with #LAUNCH_PARAM_BUFFER_POINTER;
"""
)}
"""
)}
The error #CUDA_ERROR_INVALID_VALUE will be returned if kernel parameters are specified with both {@code kernelParams} and {@code extra} (i.e. both
{@code kernelParams} and {@code extra} are non-NULL). #CUDA_ERROR_INVALID_VALUE will be returned if {@code extra} is used for a cooperative kernel.
The {@code kernelParams} or {@code extra} array, as well as the argument values it points to, are copied during this call.
${note("""Kernels launched using graphs must not use texture and surface references. Reading or writing through any texture or surface reference is
undefined behavior. This restriction does not apply to texture and surface objects.""")}
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_KERNEL_NODE_PARAMS.const.p("nodeParams", "parameters for the GPU execution node")
)
IgnoreMissing..CUresult(
"GraphKernelNodeGetParams",
"""
Returns a kernel node's parameters.
Returns the parameters of kernel node {@code hNode} in {@code nodeParams}. The {@code kernelParams} or {@code extra} array returned in
{@code nodeParams}, as well as the argument values it points to, are owned by the node. This memory remains valid until the node is destroyed or its
parameters are modified, and should not be modified directly. Use #GraphKernelNodeSetParams() to update the parameters of this node.
The params will contain either {@code kernelParams} or {@code extra}, according to which of these was most recently set on the node.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_KERNEL_NODE_PARAMS.p("nodeParams", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphKernelNodeSetParams",
"""
Sets a kernel node's parameters.
Sets the parameters of kernel node {@code hNode} to {@code nodeParams}.
""",
CUgraphNode("hNode", "node to set the parameters for"),
CUDA_KERNEL_NODE_PARAMS.const.p("nodeParams", "parameters to copy")
)
IgnoreMissing..CUresult(
"GraphAddMemcpyNode",
"""
Creates a memcpy node and adds it to a graph.
Creates a new memcpy node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies}. It is possible
for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph. {@code dependencies} may not have any duplicate
entries. A handle to the new node will be returned in {@code phGraphNode}.
When the graph is launched, the node will perform the memcpy described by {@code copyParams}. See #Memcpy3D() for a description of the structure and
its restrictions.
Memcpy nodes have some additional restrictions with regards to managed memory, if the system contains at least one device which has a zero value for
the device attribute #DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS. If one or more of the operands refer to managed memory, then using the memory
type #MEMORYTYPE_UNIFIED is disallowed for those operand(s). The managed memory will be treated as residing on either the host or the device,
depending on which memory type is specified.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_MEMCPY3D.const.p("copyParams", "parameters for the memory copy"),
CUcontext("ctx", "context on which to run the node")
)
IgnoreMissing..CUresult(
"GraphMemcpyNodeGetParams",
"""
Returns a memcpy node's parameters.
Returns the parameters of memcpy node {@code hNode} in {@code nodeParams}.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_MEMCPY3D.p("nodeParams", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphMemcpyNodeSetParams",
"""
Sets a memcpy node's parameters.
Sets the parameters of memcpy node {@code hNode} to {@code nodeParams}.
""",
CUgraphNode("hNode", "node to set the parameters for"),
CUDA_MEMCPY3D.const.p("nodeParams", "parameters to copy")
)
IgnoreMissing..CUresult(
"GraphAddMemsetNode",
"""
Creates a memset node and adds it to a graph.
Creates a new memset node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies}. It is possible
for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph. {@code dependencies} may not have any duplicate
entries. A handle to the new node will be returned in {@code phGraphNode}.
The element size must be 1, 2, or 4 bytes. When the graph is launched, the node will perform the memset described by {@code memsetParams}.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_MEMSET_NODE_PARAMS.const.p("memsetParams", "parameters for the memory set"),
CUcontext("ctx", "context on which to run the node")
)
IgnoreMissing..CUresult(
"GraphMemsetNodeGetParams",
"""
Returns a memset node's parameters.
Returns the parameters of memset node {@code hNode} in {@code nodeParams}.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_MEMSET_NODE_PARAMS.p("nodeParams", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphMemsetNodeSetParams",
"""
Sets a memset node's parameters.
Sets the parameters of memset node {@code hNode} to {@code nodeParams}.
""",
CUgraphNode("hNode", "node to set the parameters for"),
CUDA_MEMSET_NODE_PARAMS.const.p("nodeParams", "parameters to copy")
)
IgnoreMissing..CUresult(
"GraphAddHostNode",
"""
Creates a host execution node and adds it to a graph.
Creates a new CPU execution node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies} and
arguments specified in {@code nodeParams}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the
graph. {@code dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
When the graph is launched, the node will invoke the specified CPU function. Host nodes are not supported under MPS with pre-Volta GPUs.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_HOST_NODE_PARAMS.const.p("nodeParams", "parameters for the host node")
)
IgnoreMissing..CUresult(
"GraphHostNodeGetParams",
"""
Returns a host node's parameters.
Returns the parameters of host node {@code hNode} in {@code nodeParams}.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_HOST_NODE_PARAMS.p("nodeParams", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphHostNodeSetParams",
"""
Sets a host node's parameters.
Sets the parameters of host node {@code hNode} to {@code nodeParams}.
""",
CUgraphNode("hNode", "node to set the parameters for"),
CUDA_HOST_NODE_PARAMS.const.p("nodeParams", "parameters to copy")
)
IgnoreMissing..CUresult(
"GraphAddChildGraphNode",
"""
Creates a child graph node and adds it to a graph.
Creates a new node which executes an embedded graph, and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code
dependencies}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph. {@code dependencies}
may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
If {@code hGraph} contains allocation or free nodes, this call will return an error.
The node executes an embedded child graph. The child graph is cloned in this call.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUgraph("childGraph", "the graph to clone into this node")
)
IgnoreMissing..CUresult(
"GraphChildGraphNodeGetGraph",
"""
Gets a handle to the embedded graph of a child graph node.
Gets a handle to the embedded graph in a child graph node. This call does not clone the graph. Changes to the graph will be reflected in the node, and
the node retains ownership of the graph.
Allocation and free nodes cannot be added to the returned graph. Attempting to do so will return an error.
""",
CUgraphNode("hNode", "node to get the embedded graph for"),
Check(1)..CUgraph.p("phGraph", "location to store a handle to the graph")
)
IgnoreMissing..CUresult(
"GraphAddEmptyNode",
"""
Creates an empty node and adds it to a graph.
Creates a new node which performs no operation, and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code
dependencies}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph. {@code dependencies}
may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
An empty node performs no operation during execution, but can be used for transitive ordering. For example, a phased execution graph with 2 groups of n
nodes with a barrier between them can be represented using an empty node and 2*n dependency edges, rather than no empty node and n^2 dependency edges.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies")
)
IgnoreMissing..CUresult(
"GraphAddEventRecordNode",
"""
Creates an event record node and adds it to a graph.
Creates a new event record node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies} and event
specified in {@code event}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph. {@code
dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
Each launch of the graph will record {@code event} to capture execution of the node's dependencies.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUevent("event", "event for the node")
)
IgnoreMissing..CUresult(
"GraphEventRecordNodeGetEvent",
"""
Returns the event associated with an event record node.
Returns the event of event record node {@code hNode} in {@code event_out}.
""",
CUgraphNode("hNode", "node to get the event for"),
Check(1)..CUevent.p("event_out", "pointer to return the event")
)
IgnoreMissing..CUresult(
"GraphEventRecordNodeSetEvent",
"""
Sets an event record node's event.
Sets the event of event record node {@code hNode} to {@code event}.
""",
CUgraphNode("hNode", "node to set the event for"),
CUevent("event", "event to use")
)
IgnoreMissing..CUresult(
"GraphAddEventWaitNode",
"""
Creates an event wait node and adds it to a graph.
Creates a new event wait node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies} and event
specified in {@code event}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph. {@code
dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
The graph node will wait for all work captured in {@code event}. See #EventRecord() for details on what is captured by an event. {@code event} may
be from a different context or device than the launch stream.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUevent("event", "event for the node")
)
IgnoreMissing..CUresult(
"GraphEventWaitNodeGetEvent",
"""
Returns the event associated with an event wait node.
Returns the event of event wait node {@code hNode} in {@code event_out}.
""",
CUgraphNode("hNode", "node to get the event for"),
Check(1)..CUevent.p("event_out", "pointer to return the event")
)
IgnoreMissing..CUresult(
"GraphEventWaitNodeSetEvent",
"""
Sets an event wait node's event.
Sets the event of event wait node {@code hNode} to {@code event}.
""",
CUgraphNode("hNode", "node to set the event for"),
CUevent("event", "event to use")
)
IgnoreMissing..CUresult(
"GraphAddExternalSemaphoresSignalNode",
"""
Creates an external semaphore signal node and adds it to a graph.
Creates a new external semaphore signal node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies}
and arguments specified in {@code nodeParams}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of
the graph. {@code dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
Performs a signal operation on a set of externally allocated semaphore objects when the node is launched. The operation(s) will occur after all of the
node's dependencies have completed.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_EXT_SEM_SIGNAL_NODE_PARAMS.const.p("nodeParams", "parameters for the node")
)
IgnoreMissing..CUresult(
"GraphExternalSemaphoresSignalNodeGetParams",
"""
Returns an external semaphore signal node's parameters.
Returns the parameters of an external semaphore signal node {@code hNode} in {@code params_out}. The {@code extSemArray} and {@code paramsArray}
returned in {@code params_out}, are owned by the node. This memory remains valid until the node is destroyed or its parameters are modified, and should
not be modified directly. Use #GraphExternalSemaphoresSignalNodeSetParams() to update the parameters of this node.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_EXT_SEM_SIGNAL_NODE_PARAMS.p("params_out", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphExternalSemaphoresSignalNodeSetParams",
"""
Sets an external semaphore signal node's parameters.
Sets the parameters of an external semaphore signal node {@code hNode} to {@code nodeParams}.
""",
CUgraphNode("hNode", "node to set the parameters for"),
CUDA_EXT_SEM_SIGNAL_NODE_PARAMS.const.p("nodeParams", "parameters to copy")
)
IgnoreMissing..CUresult(
"GraphAddExternalSemaphoresWaitNode",
"""
Creates an external semaphore wait node and adds it to a graph.
Creates a new external semaphore wait node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies}
and arguments specified in {@code nodeParams}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of
the graph. {@code dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
Performs a wait operation on a set of externally allocated semaphore objects when the node is launched. The node's dependencies will not be launched
until the wait operation has completed.
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_EXT_SEM_WAIT_NODE_PARAMS.const.p("nodeParams", "parameters for the node")
)
IgnoreMissing..CUresult(
"GraphExternalSemaphoresWaitNodeGetParams",
"""
Returns an external semaphore wait node's parameters.
Returns the parameters of an external semaphore wait node {@code hNode} in {@code params_out}. The {@code extSemArray} and {@code paramsArray} returned
in {@code params_out}, are owned by the node. This memory remains valid until the node is destroyed or its parameters are modified, and should not be
modified directly. Use #GraphExternalSemaphoresSignalNodeSetParams() to update the parameters of this node.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_EXT_SEM_WAIT_NODE_PARAMS.p("params_out", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphExternalSemaphoresWaitNodeSetParams",
"""
Sets an external semaphore wait node's parameters.
Sets the parameters of an external semaphore wait node {@code hNode} to {@code nodeParams}.
""",
CUgraphNode("hNode", "node to set the parameters for"),
CUDA_EXT_SEM_WAIT_NODE_PARAMS.const.p("nodeParams", "parameters to copy")
)
IgnoreMissing..CUresult(
"GraphAddMemAllocNode",
"""
Creates an allocation node and adds it to a graph.
Creates a new allocation node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies} and arguments
specified in {@code nodeParams}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph.
{@code dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
When #GraphAddMemAllocNode() creates an allocation node, it returns the address of the allocation in {@code nodeParams.dptr}. The allocation's address
remains fixed across instantiations and launches.
If the allocation is freed in the same graph, by creating a free node using #GraphAddMemFreeNode(), the allocation can be accessed by nodes ordered
after the allocation node but before the free node. These allocations cannot be freed outside the owning graph, and they can only be freed once in the
owning graph.
If the allocation is not freed in the same graph, then it can be accessed not only by nodes in the graph which are ordered after the allocation node,
but also by stream operations ordered after the graph's execution but before the allocation is freed.
Allocations which are not freed in the same graph can be freed by:
${ul(
"passing the allocation to #MemFreeAsync() or #MemFree();",
"launching a graph with a free node for that allocation; or",
"""
specifying #CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH during instantiation, which makes each launch behave as though it called
#MemFreeAsync() for every unfreed allocation.
"""
)}
It is not possible to free an allocation in both the owning graph and another graph. If the allocation is freed in the same graph, a free node cannot
be added to another graph. If the allocation is freed in another graph, a free node can no longer be added to the owning graph.
The following restrictions apply to graphs which contain allocation and/or memory free nodes:
${ul(
"Nodes and edges of the graph cannot be deleted.",
"The graph cannot be used in a child node.",
"Only one instantiation of the graph may exist at any point in time.",
"The graph cannot be cloned."
)}
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUDA_MEM_ALLOC_NODE_PARAMS.p("nodeParams", "parameters for the node")
)
IgnoreMissing..CUresult(
"GraphMemAllocNodeGetParams",
"""
Returns a memory alloc node's parameters.
Returns the parameters of a memory alloc node {@code hNode} in {@code params_out}. The {@code poolProps} and {@code accessDescs} returned in {@code
params_out}, are owned by the node. This memory remains valid until the node is destroyed. The returned parameters must not be modified.
""",
CUgraphNode("hNode", "node to get the parameters for"),
CUDA_MEM_ALLOC_NODE_PARAMS.p("params_out", "pointer to return the parameters")
)
IgnoreMissing..CUresult(
"GraphAddMemFreeNode",
"""
Creates a memory free node and adds it to a graph.
Creates a new memory free node and adds it to {@code hGraph} with {@code numDependencies} dependencies specified via {@code dependencies} and arguments
specified in {@code nodeParams}. It is possible for {@code numDependencies} to be 0, in which case the node will be placed at the root of the graph.
{@code dependencies} may not have any duplicate entries. A handle to the new node will be returned in {@code phGraphNode}.
#GraphAddMemFreeNode() will return #CUDA_ERROR_INVALID_VALUE if the user attempts to free:
${ul(
"an allocation twice in the same graph.",
"an address that was not returned by an allocation node.",
"an invalid address."
)}
The following restrictions apply to graphs which contain allocation and/or memory free nodes:
${ul(
"Nodes and edges of the graph cannot be deleted.",
"The graph cannot be used in a child node.",
"Only one instantiation of the graph may exist at any point in time.",
"The graph cannot be cloned."
)}
""",
Check(1)..CUgraphNode.p("phGraphNode", "returns newly created node"),
CUgraph("hGraph", "graph to which to add the node"),
nullable..CUgraphNode.const.p("dependencies", "dependencies of the node"),
AutoSize("dependencies")..size_t("numDependencies", "number of dependencies"),
CUdeviceptr("dptr", "address of memory to free")
)
IgnoreMissing..CUresult(
"GraphMemFreeNodeGetParams",
"""
Returns a memory free node's parameters.
Returns the address of a memory free node {@code hNode} in {@code dptr_out}.
""",
CUgraphNode("hNode", "node to get the parameters for"),
Check(1)..CUdeviceptr.p("dptr_out", "pointer to return the device address")
)
IgnoreMissing..CUresult(
"DeviceGraphMemTrim",
"""
Free unused memory that was cached on the specified device for use with graphs back to the OS.
Blocks which are not in use by a graph that is either currently executing or scheduled to execute are freed back to the operating system.
""",
CUdevice("device", "the device for which cached memory should be freed")
)
IgnoreMissing..CUresult(
"DeviceGetGraphMemAttribute",
"""
Query asynchronous allocation attributes related to graphs.
Valid attributes are:
${ul(
"#GRAPH_MEM_ATTR_USED_MEM_CURRENT: Amount of memory, in bytes, currently associated with graphs",
"""
#GRAPH_MEM_ATTR_USED_MEM_HIGH: High watermark of memory, in bytes, associated with graphs since the last time it was reset. High watermark can
only be reset to zero.
""",
"#GRAPH_MEM_ATTR_RESERVED_MEM_CURRENT: Amount of memory, in bytes, currently allocated for use by the CUDA graphs asynchronous allocator.",
"#GRAPH_MEM_ATTR_RESERVED_MEM_HIGH: High watermark of memory, in bytes, currently allocated for use by the CUDA graphs asynchronous allocator."
)}
""",
CUdevice("device", "specifies the scope of the query"),
CUgraphMem_attribute("attr", "attribute to get"),
MultiType(PointerMapping.DATA_LONG)..Unsafe..void.p("value", "retrieved value")
)
IgnoreMissing..CUresult(
"DeviceSetGraphMemAttribute",
"""
Set asynchronous allocation attributes related to graphs.
Valid attributes are:
${ul(
"""
#GRAPH_MEM_ATTR_USED_MEM_HIGH: High watermark of memory, in bytes, associated with graphs since the last time it was reset. High watermark can
only be reset to zero.
""",
"#GRAPH_MEM_ATTR_RESERVED_MEM_HIGH: High watermark of memory, in bytes, currently allocated for use by the CUDA graphs asynchronous allocator."
)}
""",
CUdevice("device", "specifies the scope of the query"),
CUgraphMem_attribute("attr", "attribute to get"),
MultiType(PointerMapping.DATA_LONG)..Unsafe..void.p("value", "pointer to value to set")
)
IgnoreMissing..CUresult(
"GraphClone",
"""
Clones a graph.
This function creates a copy of {@code originalGraph} and returns it in {@code phGraphClone}. All parameters are copied into the cloned graph. The
original graph may be modified after this call without affecting the clone.
Child graph nodes in the original graph are recursively copied into the clone.
""",
Check(1)..CUgraph.p("phGraphClone", "returns newly created cloned graph"),
CUgraph("originalGraph", "graph to clone")
)
IgnoreMissing..CUresult(
"GraphNodeFindInClone",
"""
Finds a cloned version of a node.
This function returns the node in {@code hClonedGraph} corresponding to {@code hOriginalNode} in the original graph.
{@code hClonedGraph} must have been cloned from {@code hOriginalGraph} via #GraphClone(). {@code hOriginalNode} must have been in {@code
hOriginalGraph} at the time of the call to #GraphClone(), and the corresponding cloned node in {@code hClonedGraph} must not have been removed. The
cloned node is then returned via {@code phClonedNode}.
""",
Check(1)..CUgraphNode.p("phNode", "returns handle to the cloned node"),
CUgraphNode("hOriginalNode", "handle to the original node"),
CUgraph("hClonedGraph", "cloned graph to query")
)
IgnoreMissing..CUresult(
"GraphNodeGetType",
"""
Returns a node's type.
Returns the node type of {@code hNode} in {@code type}.
""",
CUgraphNode("hNode", "node to query"),
Check(1)..CUgraphNodeType.p("type", "pointer to return the node type")
)
IgnoreMissing..CUresult(
"GraphGetNodes",
"""
Returns a graph's nodes.
Returns a list of {@code hGraph's} nodes. {@code nodes} may be #NULL, in which case this function will return the number of nodes in {@code numNodes}.
Otherwise, {@code numNodes} entries will be filled in. If {@code numNodes} is higher than the actual number of nodes, the remaining entries in {@code
nodes} will be set to #NULL, and the number of nodes actually obtained will be returned in {@code numNodes}.
""",
CUgraph("hGraph", "graph to query"),
nullable..CUgraphNode.p("nodes", "pointer to return the nodes"),
AutoSize("nodes")..Check(1)..size_t.p("numNodes", "see description")
)
IgnoreMissing..CUresult(
"GraphGetRootNodes",
"""
Returns a graph's root nodes.
Returns a list of {@code hGraph's} root nodes. {@code rootNodes} may be #NULL, in which case this function will return the number of root nodes in
{@code numRootNodes}. Otherwise, {@code numRootNodes} entries will be filled in. If {@code numRootNodes} is higher than the actual number of root
nodes, the remaining entries in {@code rootNodes} will be set to #NULL, and the number of nodes actually obtained will be returned in {@code
numRootNodes}.
""",
CUgraph("hGraph", "graph to query"),
nullable..CUgraphNode.p("rootNodes", "pointer to return the root nodes"),
AutoSize("rootNodes")..Check(1)..size_t.p("numRootNodes", "see description") // TODO:
)
IgnoreMissing..CUresult(
"GraphGetEdges",
"""
Returns a graph's dependency edges.
Returns a list of {@code hGraph's} dependency edges. Edges are returned via corresponding indices in {@code from} and {@code to;} that is, the node in
{@code to[i]} has a dependency on the node in {@code from[i]}. {@code from} and {@code to} may both be #NULL, in which case this function only returns
the number of edges in {@code numEdges}. Otherwise, {@code numEdges} entries will be filled in. If {@code numEdges} is higher than the actual number of
edges, the remaining entries in {@code from} and {@code to} will be set to #NULL, and the number of edges actually returned will be written to
{@code numEdges}.
""",
CUgraph("hGraph", "graph to get the edges from"),
nullable..CUgraphNode.p("from", "location to return edge endpoints"),
nullable..CUgraphNode.p("to", "location to return edge endpoints"),
AutoSize("from", "to")..Check(1)..size_t.p("numEdges", "see description")
)
IgnoreMissing..CUresult(
"GraphNodeGetDependencies",
"""
Returns a node's dependencies.
Returns a list of {@code node's} dependencies. {@code dependencies} may be #NULL, in which case this function will return the number of dependencies in
{@code numDependencies}. Otherwise, {@code numDependencies} entries will be filled in. If {@code numDependencies} is higher than the actual number of
dependencies, the remaining entries in {@code dependencies} will be set to #NULL, and the number of nodes actually obtained will be returned in
{@code numDependencies}.
""",
CUgraphNode("hNode", "node to query"),
nullable..CUgraphNode.p("dependencies", "pointer to return the dependencies"),
AutoSize("dependencies")..Check(1)..size_t.p("numDependencies", "see description")
)
IgnoreMissing..CUresult(
"GraphNodeGetDependentNodes",
"""
Returns a node's dependent nodes.
Returns a list of {@code node's} dependent nodes. {@code dependentNodes} may be #NULL, in which case this function will return the number of dependent
nodes in {@code numDependentNodes}. Otherwise, {@code numDependentNodes} entries will be filled in. If {@code numDependentNodes} is higher than the
actual number of dependent nodes, the remaining entries in {@code dependentNodes} will be set to #NULL, and the number of nodes actually obtained will
be returned in {@code numDependentNodes}.
""",
CUgraphNode("hNode", "node to query"),
nullable..CUgraphNode.p("dependentNodes", "pointer to return the dependent nodes"),
AutoSize("dependentNodes")..Check(1)..size_t.p("numDependentNodes", "see description")
)
IgnoreMissing..CUresult(
"GraphAddDependencies",
"""
Adds dependency edges to a graph.
The number of dependencies to be added is defined by {@code numDependencies} Elements in {@code from} and {@code to} at corresponding indices define a
dependency. Each node in {@code from} and {@code to} must belong to {@code hGraph}.
If {@code numDependencies} is 0, elements in {@code from} and {@code to} will be ignored. Specifying an existing dependency will return an error.
""",
CUgraph("hGraph", "graph to which dependencies are added"),
nullable..CUgraphNode.const.p("from", "array of nodes that provide the dependencies"),
nullable..CUgraphNode.const.p("to", "array of dependent nodes"),
AutoSize("from", "to")..size_t("numDependencies", "number of dependencies to be added")
)
IgnoreMissing..CUresult(
"GraphRemoveDependencies",
"""
Removes dependency edges from a graph.
The number of {@code dependencies} to be removed is defined by {@code numDependencies}. Elements in {@code from} and {@code to} at corresponding
indices define a dependency. Each node in {@code from} and {@code to} must belong to {@code hGraph}.
If {@code numDependencies} is 0, elements in {@code from} and {@code to} will be ignored. Specifying a non-existing dependency will return an error.
Dependencies cannot be removed from graphs which contain allocation or free nodes. Any attempt to do so will return an error.
""",
CUgraph("hGraph", "graph from which to remove dependencies"),
nullable..CUgraphNode.const.p("from", "array of nodes that provide the dependencies"),
nullable..CUgraphNode.const.p("to", "array of dependent nodes"),
AutoSize("from", "to")..size_t("numDependencies", "number of dependencies to be removed")
)
IgnoreMissing..CUresult(
"GraphDestroyNode",
"""
Remove a node from the graph.
Removes {@code hNode} from its graph. This operation also severs any dependencies of other nodes on {@code hNode} and vice versa.
Nodes which belong to a graph which contains allocation or free nodes cannot be destroyed. Any attempt to do so will return an error.
""",
CUgraphNode("hNode", "node to remove")
)
IgnoreMissing..CUresult(
"GraphInstantiate",
"""
Creates an executable graph from a graph.
Instantiates {@code hGraph} as an executable graph. The graph is validated for any structural constraints or intra-node constraints which were not
previously validated. If instantiation is successful, a handle to the instantiated graph is returned in {@code phGraphExec}.
If there are any errors, diagnostic information may be returned in {@code errorNode} and {@code logBuffer}. This is the primary way to inspect
instantiation errors. The output will be null terminated unless the diagnostics overflow the buffer. In this case, they will be truncated, and the last
byte can be inspected to determine if truncation occurred.
""",
Check(1)..CUgraphExec.p("phGraphExec", "returns instantiated graph"),
CUgraph("hGraph", "graph to instantiate"),
Check(1)..CUgraphNode.p("phErrorNode", "in case of an instantiation error, this may be modified to indicate a node contributing to the error"),
char.p("logBuffer", "a character buffer to store diagnostic messages"),
AutoSize("logBuffer")..size_t("bufferSize", "size of the log buffer in bytes")
).versioned()
IgnoreMissing..CUresult(
"GraphInstantiateWithFlags",
"""
Creates an executable graph from a graph.
Instantiates {@code hGraph} as an executable graph. The graph is validated for any structural constraints or intra-node constraints which were not
previously validated. If instantiation is successful, a handle to the instantiated graph is returned in {@code phGraphExec}.
The {@code flags} parameter controls the behavior of instantiation and subsequent graph launches. Valid flags are:
${ul(
"""
#CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH, which configures a graph containing memory allocation nodes to automatically free any unfreed
memory allocations before the graph is relaunched.
"""
)}
If {@code hGraph} contains any allocation or free nodes, there can be at most one executable graph in existence for that graph at a time.
An attempt to instantiate a second executable graph before destroying the first with #GraphExecDestroy() will result in an error.
""",
Check(1)..CUgraphExec.p("phGraphExec", "returns instantiated graph"),
CUgraph("hGraph", "graph to instantiate"),
unsigned_long_long("flags", "flags to control instantiation. See {@code CUgraphInstantiate_flags}.")
)
IgnoreMissing..CUresult(
"GraphExecKernelNodeSetParams",
"""
Sets the parameters for a kernel node in the given {@code graphExec}.
Sets the parameters of a kernel node in an executable graph {@code hGraphExec}. The node is identified by the corresponding node {@code hNode} in the
non-executable graph, from which the executable graph was instantiated.
{@code hNode} must not have been removed from the original graph. The {@code func} field of {@code nodeParams} cannot be modified and must match the
original value. All other values can be modified.
The modifications take effect at the next launch of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. {@code hNode} is also not modified by this call.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "kernel node from the graph from which graphExec was instantiated"),
CUDA_KERNEL_NODE_PARAMS.const.p("nodeParams", "updated parameters to set")
)
IgnoreMissing..CUresult(
"GraphExecMemcpyNodeSetParams",
"""
Sets the parameters for a memcpy node in the given {@code graphExec}.
Updates the work represented by {@code hNode} in {@code hGraphExec} as though {@code hNode} had contained {@code copyParams} at instantiation.
{@code hNode} must remain in the graph which was used to instantiate {@code hGraphExec}. Changed edges to and from {@code hNode} are ignored.
The source and destination memory in {@code copyParams} must be allocated from the same contexts as the original source and destination memory. Both
the instantiation-time memory operands and the memory operands in {@code copyParams} must be 1-dimensional. Zero-length operations are not supported.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. hNode is also not modified by this call.
Returns #CUDA_ERROR_INVALID_VALUE if the memory operands' mappings changed or either the original or new memory operands are multidimensional.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "memcpy node from the graph which was used to instantiate graphExec"),
CUDA_MEMCPY3D.const.p("copyParams", "the updated parameters to set"),
CUcontext("ctx", "context on which to run the node")
)
IgnoreMissing..CUresult(
"GraphExecMemsetNodeSetParams",
"""
Sets the parameters for a {@code memset} node in the given {@code graphExec}.
Updates the work represented by {@code hNode} in {@code hGraphExec} as though {@code hNode} had contained {@code memsetParams} at instantiation.
{@code hNode} must remain in the graph which was used to instantiate {@code hGraphExec}. Changed edges to and from {@code hNode} are ignored.
The destination memory in {@code memsetParams} must be allocated from the same contexts as the original destination memory. Both the instantiation-time
memory operand and the memory operand in {@code memsetParams} must be 1-dimensional. Zero-length operations are not supported.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. hNode is also not modified by this call.
Returns CUDA_ERROR_INVALID_VALUE if the memory operand's mappings changed or either the original or new memory operand are multidimensional.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "memset node from the graph which was used to instantiate graphExec"),
CUDA_MEMSET_NODE_PARAMS.const.p("memsetParams", "the updated parameters to set"),
CUcontext("ctx", "context on which to run the node")
)
IgnoreMissing..CUresult(
"GraphExecHostNodeSetParams",
"""
Sets the parameters for a host node in the given {@code graphExec}.
Updates the work represented by {@code hNode} in {@code hGraphExec} as though {@code hNode} had contained {@code nodeParams} at instantiation.
{@code hNode} must remain in the graph which was used to instantiate {@code hGraphExec}. Changed edges to and from {@code hNode} are ignored.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. hNode is also not modified by this call.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "host node from the graph which was used to instantiate graphExec"),
CUDA_HOST_NODE_PARAMS.const.p("nodeParams", "the updated parameters to set")
)
IgnoreMissing..CUresult(
"GraphExecChildGraphNodeSetParams",
"""
Updates node parameters in the child graph node in the given {@code graphExec}.
Updates the work represented by {@code hNode} in {@code hGraphExec} as though the nodes contained in {@code hNode's} graph had the parameters contained
in {@code childGraph's} nodes at instantiation. {@code hNode} must remain in the graph which was used to instantiate {@code hGraphExec}. Changed edges
to and from {@code hNode} are ignored.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. {@code hNode} is also not modified by this call.
The topology of {@code childGraph}, as well as the node insertion order, must match that of the graph contained in {@code hNode}. See
#GraphExecUpdate() for a list of restrictions on what can be updated in an instantiated graph. The update is recursive, so child graph nodes
contained within the top level child graph will also be updated.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "host node from the graph which was used to instantiate {@code graphExec}"),
CUgraph("childGraph", "the graph supplying the updated parameters")
)
IgnoreMissing..CUresult(
"GraphExecEventRecordNodeSetEvent",
"""
Sets the event for an event record node in the given {@code graphExec}.
Sets the event of an event record node in an executable graph {@code hGraphExec}. The node is identified by the corresponding node {@code hNode} in the
non-executable graph, from which the executable graph was instantiated.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. {@code hNode} is also not modified by this call.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "event record node from the graph from which graphExec was instantiated"),
CUevent("event", "updated event to use")
)
IgnoreMissing..CUresult(
"GraphExecEventWaitNodeSetEvent",
"""
Sets the event for an event wait node in the given {@code graphExec}.
Sets the event of an event wait node in an executable graph {@code hGraphExec}. The node is identified by the corresponding node {@code hNode} in the
non-executable graph, from which the executable graph was instantiated.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. {@code hNode} is also not modified by this call.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "event wait node from the graph from which graphExec was instantiated"),
CUevent("event", "updated event to use")
)
IgnoreMissing..CUresult(
"GraphExecExternalSemaphoresSignalNodeSetParams",
"""
Sets the parameters for an external semaphore signal node in the given {@code graphExec}.
Sets the parameters of an external semaphore signal node in an executable graph {@code hGraphExec}. The node is identified by the corresponding node
{@code hNode} in the non-executable graph, from which the executable graph was instantiated.
{@code hNode} must not have been removed from the original graph.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. {@code hNode} is also not modified by this call.
Changing {@code nodeParams->numExtSems} is not supported.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "semaphore signal node from the graph from which graphExec was instantiated"),
CUDA_EXT_SEM_SIGNAL_NODE_PARAMS.const.p("nodeParams", "updated Parameters to set")
)
IgnoreMissing..CUresult(
"GraphExecExternalSemaphoresWaitNodeSetParams",
"""
Sets the parameters for an external semaphore wait node in the given graphExec.
Sets the parameters of an external semaphore wait node in an executable graph {@code hGraphExec}. The node is identified by the corresponding node
{@code hNode} in the non-executable graph, from which the executable graph was instantiated.
{@code hNode} must not have been removed from the original graph.
The modifications only affect future launches of {@code hGraphExec}. Already enqueued or running launches of {@code hGraphExec} are not affected by
this call. {@code hNode} is also not modified by this call.
Changing {@code nodeParams->numExtSems} is not supported.
""",
CUgraphExec("hGraphExec", "the executable graph in which to set the specified node"),
CUgraphNode("hNode", "semaphore wait node from the graph from which graphExec was instantiated"),
CUDA_EXT_SEM_WAIT_NODE_PARAMS.const.p("nodeParams", "updated Parameters to set")
)
IgnoreMissing..CUresult(
"GraphUpload",
"""
Uploads an executable graph in a stream.
Uploads {@code hGraphExec} to the device in {@code hStream} without executing it. Uploads of the same {@code hGraphExec} will be serialized. Each
upload is ordered behind both any previous work in {@code hStream} and any previous launches of {@code hGraphExec}. Uses memory cached by {@code
stream} to back the allocations owned by {@code hGraphExec}.
""",
CUgraphExec("hGraphExec", "executable graph to upload"),
nullable..CUstream("hStream", "stream in which to upload the graph")
).ptsz()
IgnoreMissing..CUresult(
"GraphLaunch",
"""
Launches an executable graph in a stream.
Executes {@code hGraphExec} in {@code hStream}. Only one instance of {@code hGraphExec} may be executing at a time. Each launch is ordered behind both
any previous work in {@code hStream} and any previous launches of {@code hGraphExec}. To execute a graph concurrently, it must be instantiated multiple
times into multiple executable graphs.
If any allocations created by {@code hGraphExec} remain unfreed (from a previous launch) and {@code hGraphExec} was not instantiated with
#CUDA_GRAPH_INSTANTIATE_FLAG_AUTO_FREE_ON_LAUNCH, the launch will fail with #CUDA_ERROR_INVALID_VALUE.
""",
CUgraphExec("hGraphExec", "executable graph to launch"),
nullable..CUstream("hStream", "stream in which to launch the graph")
).ptsz()
IgnoreMissing..CUresult(
"GraphExecDestroy",
"""
Destroys an executable graph.
Destroys the executable graph specified by {@code hGraphExec}, as well as all of its executable nodes. If the executable graph is in-flight, it will
not be terminated, but rather freed asynchronously on completion.
""",
CUgraphExec("hGraphExec", "executable graph to destroy")
)
IgnoreMissing..CUresult(
"GraphDestroy",
"""
Destroys a graph.
Destroys the graph specified by {@code hGraph}, as well as all of its nodes.
""",
CUgraph("hGraph", "graph to destroy")
)
IgnoreMissing..CUresult(
"GraphExecUpdate",
"""
Check whether an executable graph can be updated with a graph and perform the update if possible.
Updates the node parameters in the instantiated graph specified by {@code hGraphExec} with the node parameters in a topologically identical graph
specified by {@code hGraph}.
Limitations:
${ul(
"""
Kernel nodes:
${ul(
"The owning context of the function cannot change.",
"A node whose function originally did not use CUDA dynamic parallelism cannot be updated to a function which uses CDP"
)}
""",
"""
Memset and memcpy nodes:
${ul(
"The CUDA device(s) to which the operand(s) was allocated/mapped cannot change.",
"The source/destination memory must be allocated from the same contexts as the original source/destination memory.",
"Only 1D memsets can be changed."
)}
""",
"""
Additional memcpy node restrictions:
${ul(
"Changing either the source or destination memory type(i.e. CU_MEMORYTYPE_DEVICE, CU_MEMORYTYPE_ARRAY, etc.) is not supported."
)}
""",
"""
External semaphore wait nodes and record nodes:
${ul(
"Changing either the source or destination memory type(i.e. CU_MEMORYTYPE_DEVICE, CU_MEMORYTYPE_ARRAY, etc.) is not supported."
)}
"""
)}
Note: The API may add further restrictions in future releases. The return code should always be checked.
{@code cuGraphExecUpdate} sets {@code updateResult_out} to #GRAPH_EXEC_UPDATE_ERROR_TOPOLOGY_CHANGED under the following conditions:
${ul(
"The count of nodes directly in {@code hGraphExec} and {@code hGraph} differ, in which case {@code hErrorNode_out} is #NULL.",
"A node is deleted in {@code hGraph} but not not its pair from {@code hGraphExec}, in which case {@code hErrorNode_out} is #NULL.",
"""
A node is deleted in {@code hGraphExec} but not its pair from {@code hGraph}, in which case {@code hErrorNode_out} is the pairless node from {@code
hGraph}.
""",
"The dependent nodes of a pair differ, in which case {@code hErrorNode_out} is the node from {@code hGraph}."
)}
{@code cuGraphExecUpdate} sets {@code updateResult_out} to:
${ul(
"#GRAPH_EXEC_UPDATE_ERROR if passed an invalid value.",
"#GRAPH_EXEC_UPDATE_ERROR_TOPOLOGY_CHANGED if the graph topology changed",
"""
#GRAPH_EXEC_UPDATE_ERROR_NODE_TYPE_CHANGED if the type of a node changed, in which case {@code hErrorNode_out} is set to the node from
{@code hGraph}.
""",
"""
#GRAPH_EXEC_UPDATE_ERROR_UNSUPPORTED_FUNCTION_CHANGE if the function changed in an unsupported way(see note above), in which case
{@code hErrorNode_out} is set to the node from {@code hGraph}
""",
"""
#GRAPH_EXEC_UPDATE_ERROR_PARAMETERS_CHANGED if any parameters to a node changed in a way that is not supported, in which case
{@code hErrorNode_out} is set to the node from {@code hGraph}.
""",
"""
#GRAPH_EXEC_UPDATE_ERROR_NOT_SUPPORTED if something about a node is unsupported, like the node's type or configuration, in which case
{@code hErrorNode_out} is set to the node from {@code hGraph}
"""
)}
If {@code updateResult_out} isn't set in one of the situations described above, the update check passes and cuGraphExecUpdate updates
{@code hGraphExec} to match the contents of {@code hGraph}. If an error happens during the update, {@code updateResult_out} will be set to
#GRAPH_EXEC_UPDATE_ERROR; otherwise, {@code updateResult_out} is set to #GRAPH_EXEC_UPDATE_SUCCESS.
{@code cuGraphExecUpdate} returns #CUDA_SUCCESS when the updated was performed successfully. It returns #CUDA_ERROR_GRAPH_EXEC_UPDATE_FAILURE if the
graph update was not performed because it included changes which violated constraints specific to instantiated graph update.
""",
CUgraphExec("hGraphExec", "the instantiated graph to be updated"),
CUgraph("hGraph", "the graph containing the updated parameters"),
Check(1)..CUgraphNode.p("hErrorNode_out", "the node which caused the permissibility check to forbid the update, if any"),
Check(1)..CUgraphExecUpdateResult.p("updateResult_out", "whether the graph update was permitted. If was forbidden, the reason why.")
)
IgnoreMissing..CUresult(
"GraphKernelNodeCopyAttributes",
"""
Copies attributes from source node to destination node.
Copies attributes from source node {@code src} to destination node {@code dst}. Both node must have the same context.
""",
CUgraphNode("dst", "destination node"),
CUgraphNode("src", "source node. For list of attributes see {@code CUkernelNodeAttrID}.")
)
IgnoreMissing..CUresult(
"GraphKernelNodeGetAttribute",
"""
Queries node attribute.
Queries attribute {@code attr} from node {@code hNode} and stores it in corresponding member of {@code value_out}.
""",
CUgraphNode("hNode", ""),
CUkernelNodeAttrID("attr", ""),
CUkernelNodeAttrValue.p("value_out", "")
)
IgnoreMissing..CUresult(
"GraphKernelNodeSetAttribute",
"""
Sets node attribute.
Sets attribute {@code attr} on node {@code hNode} from corresponding attribute of {@code value}.
""",
CUgraphNode("hNode", ""),
CUkernelNodeAttrID("attr", ""),
CUkernelNodeAttrValue.const.p("value", "")
)
IgnoreMissing..CUresult(
"GraphDebugDotPrint",
"""
Write a DOT file describing graph structure.
Using the provided {@code hGraph}, write to {@code path} a DOT formatted description of the graph. By default this includes the graph topology, node
types, node id, kernel names and memcpy direction. {@code flags} can be specified to write more detailed information about each node type such as
parameter values, kernel attributes, node and function handles.
""",
CUgraph("hGraph", "the graph to create a DOT file from"),
charUTF8.const.p("path", "the path to write the DOT file to"),
unsigned_int("flags", "flags from {@code CUgraphDebugDot_flags} for specifying which additional node information to write")
)
IgnoreMissing..CUresult(
"UserObjectCreate",
"""
Create a user object.
Create a user object with the specified destructor callback and initial reference count. The initial references are owned by the caller.
Destructor callbacks cannot make CUDA API calls and should avoid blocking behavior, as they are executed by a shared internal thread. Another thread
may be signaled to perform such actions, if it does not block forward progress of tasks scheduled through CUDA.
See CUDA User Objects in the CUDA C++ Programming Guide for more information on user objects.
""",
Check(1)..CUuserObject.p("object_out", "location to return the user object handle"),
opaque_p("ptr", "the pointer to pass to the destroy function"),
CUhostFn("destroy", "callback to free the user object when it is no longer in use"),
unsigned_int(
"initialRefcount",
"the initial refcount to create the object with, typically 1. The initial references are owned by the calling thread."
),
unsigned_int(
"flags",
"""
currently it is required to pass #USER_OBJECT_NO_DESTRUCTOR_SYNC, which is the only defined flag. This indicates that the destroy callback
cannot be waited on by any CUDA API. Users requiring synchronization of the callback should signal its completion manually.
"""
)
)
IgnoreMissing..CUresult(
"UserObjectRetain",
"""
Retain a reference to a user object.
Retains new references to a user object. The new references are owned by the caller.
See CUDA User Objects in the CUDA C++ Programming Guide for more information on user objects.
""",
CUuserObject("object", "the object to retain"),
unsigned_int("count", "the number of references to retain, typically 1. Must be nonzero and not larger than INT_MAX.")
)
IgnoreMissing..CUresult(
"UserObjectRelease",
"""
Release a reference to a user object.
Releases user object references owned by the caller. The object's destructor is invoked if the reference count reaches zero.
It is undefined behavior to release references not owned by the caller, or to use a user object handle after all references are released.
See CUDA User Objects in the CUDA C++ Programming Guide for more information on user objects.
""",
CUuserObject("object", "the object to release"),
unsigned_int("count", "the number of references to release, typically 1. Must be nonzero and not larger than INT_MAX.")
)
IgnoreMissing..CUresult(
"GraphRetainUserObject",
"""
Retain a reference to a user object from a graph.
Creates or moves user object references that will be owned by a CUDA graph.
See CUDA User Objects in the CUDA C++ Programming Guide for more information on user objects.
""",
CUgraph("graph", "the graph to associate the reference with"),
CUuserObject("object", "the user object to retain a reference for"),
unsigned_int("count", "the number of references to add to the graph, typically 1. Must be nonzero and not larger than INT_MAX."),
unsigned_int(
"flags",
"""
the optional flag #GRAPH_USER_OBJECT_MOVE transfers references from the calling thread, rather than create new references. Pass 0 to create new
references.
"""
)
)
IgnoreMissing..CUresult(
"GraphReleaseUserObject",
"""
Release a user object reference from a graph.
Releases user object references owned by a graph.
See CUDA User Objects in the CUDA C++ Programming Guide for more information on user objects.
""",
CUgraph("graph", "the graph that will release the reference"),
CUuserObject("object", "the user object to release a reference for"),
unsigned_int("count", "the number of references to release, typically 1. Must be nonzero and not larger than INT_MAX.")
)
IgnoreMissing..CUresult(
"OccupancyMaxActiveBlocksPerMultiprocessor",
"""
Returns occupancy of a function.
Returns in {@code *numBlocks} the number of the maximum active blocks per streaming multiprocessor.
""",
Check(1)..int.p("numBlocks", "returned occupancy"),
CUfunction("func", "kernel for which occupancy is calculated"),
int("blockSize", "block size the kernel is intended to be launched with"),
size_t("dynamicSMemSize", "per-block dynamic shared memory usage intended, in bytes")
)
IgnoreMissing..CUresult(
"OccupancyMaxActiveBlocksPerMultiprocessorWithFlags",
"""
Returns occupancy of a function.
Returns in {@code *numBlocks} the number of the maximum active blocks per streaming multiprocessor.
The {@code Flags} parameter controls how special cases are handled. The valid flags are:
${ul(
"#OCCUPANCY_DEFAULT, which maintains the default behavior as #OccupancyMaxActiveBlocksPerMultiprocessor();",
"""
#OCCUPANCY_DISABLE_CACHING_OVERRIDE, which suppresses the default behavior on platform where global caching affects occupancy. On such
platforms, if caching is enabled, but per-block SM resource usage would result in zero occupancy, the occupancy calculator will calculate the
occupancy as if caching is disabled. Setting #OCCUPANCY_DISABLE_CACHING_OVERRIDE makes the occupancy calculator to return 0 in such cases. More
information can be found about this feature in the "Unified L1/Texture Cache" section of the Maxwell tuning guide.
"""
)}
""",
Check(1)..int.p("numBlocks", "returned occupancy"),
CUfunction("func", "kernel for which occupancy is calculated"),
int("blockSize", "block size the kernel is intended to be launched with"),
size_t("dynamicSMemSize", "per-block dynamic shared memory usage intended, in bytes"),
unsigned_int("flags", "requested behavior for the occupancy calculator")
)
IgnoreMissing..CUresult(
"OccupancyMaxPotentialBlockSize",
"""
Suggest a launch configuration with reasonable occupancy.
Returns in {@code *blockSize} a reasonable block size that can achieve the maximum occupancy (or, the maximum number of active warps with the fewest
blocks per multiprocessor), and in {@code *minGridSize} the minimum grid size to achieve the maximum occupancy.
If {@code blockSizeLimit} is 0, the configurator will use the maximum block size permitted by the device / function instead.
If per-block dynamic shared memory allocation is not needed, the user should leave both {@code blockSizeToDynamicSMemSize} and {@code dynamicSMemSize}
as 0.
If per-block dynamic shared memory allocation is needed, then if the dynamic shared memory size is constant regardless of block size, the size should
be passed through {@code dynamicSMemSize}, and {@code blockSizeToDynamicSMemSize} should be #NULL.
Otherwise, if the per-block dynamic shared memory size varies with different block sizes, the user needs to provide a unary function through {@code
blockSizeToDynamicSMemSize} that computes the dynamic shared memory needed by {@code func} for any given block size. {@code dynamicSMemSize} is
ignored. An example signature is:
${codeBlock("""
// Take block size, returns dynamic shared memory needed
size_t blockToSmem(int blockSize);""")}
""",
Check(1)..int.p("minGridSize", "returned minimum grid size needed to achieve the maximum occupancy"),
Check(1)..int.p("blockSize", "returned maximum block size that can achieve the maximum occupancy"),
CUfunction("func", "kernel for which launch configuration is calculated"),
nullable..CUoccupancyB2DSize(
"blockSizeToDynamicSMemSize",
"a function that calculates how much per-block dynamic shared memory {@code func} uses based on the block size"
),
size_t("dynamicSMemSize", "dynamic shared memory usage intended, in bytes"),
int("blockSizeLimit", "the maximum block size {@code func} is designed to handle")
)
IgnoreMissing..CUresult(
"OccupancyMaxPotentialBlockSizeWithFlags",
"""
Suggest a launch configuration with reasonable occupancy.
An extended version of #OccupancyMaxPotentialBlockSize(). In addition to arguments passed to #OccupancyMaxPotentialBlockSize(),
#OccupancyMaxPotentialBlockSizeWithFlags() also takes a {@code Flags} parameter.
The {@code Flags} parameter controls how special cases are handled. The valid flags are:
${ul(
"#OCCUPANCY_DEFAULT, which maintains the default behavior as #OccupancyMaxPotentialBlockSize();",
"""
#OCCUPANCY_DISABLE_CACHING_OVERRIDE, which suppresses the default behavior on platform where global caching affects occupancy. On such
platforms, the launch configurations that produces maximal occupancy might not support global caching. Setting
#OCCUPANCY_DISABLE_CACHING_OVERRIDE guarantees that the the produced launch configuration is global caching compatible at a potential cost of
occupancy. More information can be found about this feature in the "Unified L1/Texture Cache" section of the Maxwell tuning guide.
"""
)}
""",
Check(1)..int.p("minGridSize", "returned minimum grid size needed to achieve the maximum occupancy"),
Check(1)..int.p("blockSize", "returned maximum block size that can achieve the maximum occupancy"),
CUfunction("func", "kernel for which launch configuration is calculated"),
nullable..CUoccupancyB2DSize(
"blockSizeToDynamicSMemSize",
"a function that calculates how much per-block dynamic shared memory {@code func} uses based on the block size"
),
size_t("dynamicSMemSize", "dynamic shared memory usage intended, in bytes"),
int("blockSizeLimit", "the maximum block size {@code func} is designed to handle"),
unsigned_int("flags", "options")
)
IgnoreMissing..CUresult(
"OccupancyAvailableDynamicSMemPerBlock",
"""
Returns dynamic shared memory available per block when launching {@code numBlocks} blocks on SM.
Returns in {@code *dynamicSmemSize} the maximum size of dynamic shared memory to allow {@code numBlocks} blocks per SM.
""",
Check(1)..size_t.p("dynamicSmemSize", "returned maximum dynamic shared memory"),
CUfunction("func", "kernel function for which occupancy is calculated"),
int("numBlocks", "number of blocks to fit on SM"),
int("blockSize", "size of the blocks")
)
CUresult(
"TexRefSetArray",
"""
Binds an array as a texture reference. (Deprecated)
Binds the CUDA array {@code hArray} to the texture reference {@code hTexRef}. Any previous address or CUDA array state associated with the texture
reference is superseded by this function. {@code Flags} must be set to #TRSA_OVERRIDE_FORMAT. Any CUDA array previously bound to {@code hTexRef} is
unbound.
""",
CUtexref("hTexRef", "texture reference to bind"),
CUarray("hArray", "array to bind"),
unsigned_int("Flags", "options (must be #TRSA_OVERRIDE_FORMAT)")
)
CUresult(
"TexRefSetMipmappedArray",
"""
Binds a mipmapped array to a texture reference. (Deprecated)
Binds the CUDA mipmapped array {@code hMipmappedArray} to the texture reference {@code hTexRef}. Any previous address or CUDA array state associated
with the texture reference is superseded by this function. {@code Flags} must be set to #TRSA_OVERRIDE_FORMAT. Any CUDA array previously bound to
{@code hTexRef} is unbound.
""",
CUtexref("hTexRef", "texture reference to bind"),
CUmipmappedArray("hMipmappedArray", "mipmapped array to bind"),
unsigned_int("Flags", "options (must be #TRSA_OVERRIDE_FORMAT)")
)
CUresult(
"TexRefSetAddress",
"""
Binds an address as a texture reference. (Deprecated)
Binds a linear address range to the texture reference {@code hTexRef}. Any previous address or CUDA array state associated with the texture reference
is superseded by this function. Any memory previously bound to {@code hTexRef} is unbound.
Since the hardware enforces an alignment requirement on texture base addresses, #TexRefSetAddress() passes back a byte offset in {@code *ByteOffset}
that must be applied to texture fetches in order to read from the desired memory. This offset must be divided by the texel size and passed to kernels
that read from the texture so they can be applied to the {@code tex1Dfetch()} function.
If the device memory pointer was returned from #MemAlloc(), the offset is guaranteed to be 0 and #NULL may be passed as the {@code ByteOffset}
parameter.
The total number of elements (or texels) in the linear address range cannot exceed #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_LINEAR_WIDTH. The number of
elements is computed as ({@code bytes} / {@code bytesPerElement}), where {@code bytesPerElement} is determined from the data format and number of
components set using #TexRefSetFormat().
""",
Check(1)..size_t.p("ByteOffset", "returned byte offset"),
CUtexref("hTexRef", "texture reference to bind"),
CUdeviceptr("dptr", "device pointer to bind"),
size_t("bytes", "size of memory to bind in bytes")
).versioned()
CUresult(
"TexRefSetAddress2D",
"""
Binds an address as a 2D texture reference. (Deprecated)
Binds a linear address range to the texture reference {@code hTexRef}. Any previous address or CUDA array state associated with the texture reference
is superseded by this function. Any memory previously bound to {@code hTexRef} is unbound.
Using a {@code tex2D()} function inside a kernel requires a call to either #TexRefSetArray() to bind the corresponding texture reference to an array,
or #TexRefSetAddress2D() to bind the texture reference to linear memory.
Function calls to #TexRefSetFormat() cannot follow calls to #TexRefSetAddress2D() for the same texture reference.
It is required that {@code dptr} be aligned to the appropriate hardware-specific texture alignment. You can query this value using the device attribute
#DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT. If an unaligned {@code dptr} is supplied, #CUDA_ERROR_INVALID_VALUE is returned.
{@code Pitch} has to be aligned to the hardware-specific texture pitch alignment. This value can be queried using the device attribute
#DEVICE_ATTRIBUTE_TEXTURE_PITCH_ALIGNMENT. If an unaligned {@code Pitch} is supplied, #CUDA_ERROR_INVALID_VALUE is returned.
{@code Width} and {@code Height}, which are specified in elements (or texels), cannot exceed #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_WIDTH and
#DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_HEIGHT respectively. {@code Pitch}, which is specified in bytes, cannot exceed
#DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_PITCH.
""",
CUtexref("hTexRef", "texture reference to bind"),
CUDA_ARRAY_DESCRIPTOR.const.p("desc", "descriptor of CUDA array"),
CUdeviceptr("dptr", "device pointer to bind"),
size_t("Pitch", "line pitch in bytes")
).versioned(3)
CUresult(
"TexRefSetFormat",
"""
Sets the format for a texture reference. (Deprecated)
Specifies the format of the data to be read by the texture reference {@code hTexRef}. {@code fmt} and {@code NumPackedComponents} are exactly analogous
to the {@code Format} and {@code NumChannels} members of the ##CUDA_ARRAY_DESCRIPTOR structure: They specify the format of each component and the
number of components per array element.
""",
CUtexref("hTexRef", "texture reference"),
CUarray_format("fmt", "format to set"),
int("NumPackedComponents", "number of components per array element")
)
CUresult(
"TexRefSetAddressMode",
"""
Sets the addressing mode for a texture reference. (Deprecated)
Specifies the addressing mode {@code am} for the given dimension {@code dim} of the texture reference {@code hTexRef}. If {@code dim} is zero, the
addressing mode is applied to the first parameter of the functions used to fetch from the texture; if {@code dim} is 1, the second, and so on.
Note that this call has no effect if {@code hTexRef} is bound to linear memory. Also, if the flag, #TRSF_NORMALIZED_COORDINATES, is not set, the
only supported address mode is #TR_ADDRESS_MODE_CLAMP.
""",
CUtexref("hTexRef", "texture reference"),
int("dim", "dimension"),
CUaddress_mode("am", "addressing mode to set")
)
CUresult(
"TexRefSetFilterMode",
"""
Sets the filtering mode for a texture reference. (Deprecated)
Specifies the filtering mode {@code fm} to be used when reading memory through the texture reference {@code hTexRef}.
Note that this call has no effect if {@code hTexRef} is bound to linear memory.
""",
CUtexref("hTexRef", "texture reference"),
CUfilter_mode("fm", "filtering mode to set")
)
CUresult(
"TexRefSetMipmapFilterMode",
"""
Sets the mipmap filtering mode for a texture reference (Deprecated)
Specifies the mipmap filtering mode {@code fm} to be used when reading memory through the texture reference {@code hTexRef}.
Note that this call has no effect if {@code hTexRef} is not bound to a mipmapped array.
""",
CUtexref("hTexRef", "texture reference"),
CUfilter_mode("fm", "filtering mode to set")
)
CUresult(
"TexRefSetMipmapLevelBias",
"""
Sets the mipmap level bias for a texture reference. (Deprecated)
Specifies the mipmap level bias {@code bias} to be added to the specified mipmap level when reading memory through the texture reference
{@code hTexRef}.
Note that this call has no effect if {@code hTexRef} is not bound to a mipmapped array.
""",
CUtexref("hTexRef", "texture reference"),
float("bias", "mipmap level bias")
)
CUresult(
"TexRefSetMipmapLevelClamp",
"""
Sets the mipmap min/max mipmap level clamps for a texture reference. (Deprecated)
Specifies the min/max mipmap level clamps, {@code minMipmapLevelClamp} and {@code maxMipmapLevelClamp} respectively, to be used when reading memory
through the texture reference {@code hTexRef}.
Note that this call has no effect if {@code hTexRef} is not bound to a mipmapped array.
""",
CUtexref("hTexRef", "texture reference"),
float("minMipmapLevelClamp", "mipmap min level clamp"),
float("maxMipmapLevelClamp", "mipmap max level clamp")
)
CUresult(
"TexRefSetMaxAnisotropy",
"""
Sets the maximum anisotropy for a texture reference. (Deprecated)
Specifies the maximum anisotropy {@code maxAniso} to be used when reading memory through the texture reference {@code hTexRef}.
Note that this call has no effect if {@code hTexRef} is bound to linear memory.
""",
CUtexref("hTexRef", "texture reference"),
unsigned_int("maxAniso", "maximum anisotropy")
)
CUresult(
"TexRefSetBorderColor",
"""
Sets the border color for a texture reference. (Deprecated)
Specifies the value of the RGBA color via the {@code pBorderColor} to the texture reference {@code hTexRef}. The color value supports only float type
and holds color components in the following sequence: {@code pBorderColor[0]} holds 'R' component {@code pBorderColor[1]} holds 'G' component
{@code pBorderColor[2]} holds 'B' component {@code pBorderColor[3]} holds 'A' component.
Note that the color values can be set only when the Address mode is set to #TR_ADDRESS_MODE_BORDER using #TexRefSetAddressMode(). Applications using
integer border color values have to "reinterpret_cast" their values to float.
""",
CUtexref("hTexRef", "texture reference"),
Check(4)..float.p("pBorderColor", "RGBA color")
)
CUresult(
"TexRefSetFlags",
"""
Sets the flags for a texture reference. (Deprecated)
Specifies optional flags via {@code Flags} to specify the behavior of data returned through the texture reference {@code hTexRef}. The valid flags are:
${ul(
"""
#TRSF_READ_AS_INTEGER, which suppresses the default behavior of having the texture promote integer data to floating point data in the range [0,
1]. Note that texture with 32-bit integer format would not be promoted, regardless of whether or not this flag is specified;
""",
"""
#TRSF_NORMALIZED_COORDINATES, which suppresses the default behavior of having the texture coordinates range from [0, Dim) where Dim is the
width or height of the CUDA array. Instead, the texture coordinates [0, 1.0) reference the entire breadth of the array dimension;
""",
"""
#TRSF_DISABLE_TRILINEAR_OPTIMIZATION, which disables any trilinear filtering optimizations. Trilinear optimizations improve texture filtering
performance by allowing bilinear filtering on textures in scenarios where it can closely approximate the expected results.
"""
)}
""",
CUtexref("hTexRef", "texture reference"),
unsigned_int("Flags", "optional flags to set")
)
CUresult(
"TexRefGetAddress",
"""
Gets the address associated with a texture reference. (Deprecated)
Returns in {@code *pdptr} the base address bound to the texture reference {@code hTexRef}, or returns #CUDA_ERROR_INVALID_VALUE if the texture
reference is not bound to any device memory range.
""",
Check(1)..CUdeviceptr.p("pdptr", "returned device address"),
CUtexref("hTexRef", "texture reference")
).versioned()
CUresult(
"TexRefGetArray",
"""
Gets the array bound to a texture reference. (Deprecated)
Returns in {@code *phArray} the CUDA array bound to the texture reference {@code hTexRef}, or returns #CUDA_ERROR_INVALID_VALUE if the texture
reference is not bound to any CUDA array.
""",
Check(1)..CUarray.p("phArray", "returned array"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetMipmappedArray",
"""
Gets the mipmapped array bound to a texture reference. (Deprecated)
Returns in {@code *phMipmappedArray} the CUDA mipmapped array bound to the texture reference {@code hTexRef}, or returns #CUDA_ERROR_INVALID_VALUE if
the texture reference is not bound to any CUDA mipmapped array.
""",
Check(1)..CUmipmappedArray.p("phMipmappedArray", "returned mipmapped array"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetAddressMode",
"""
Gets the addressing mode used by a texture reference. (Deprecated)
Returns in {@code *pam} the addressing mode corresponding to the dimension {@code dim} of the texture reference {@code hTexRef}. Currently, the only
valid value for {@code dim} are 0 and 1.
""",
Check(1)..CUaddress_mode.p("pam", "returned addressing mode"),
CUtexref("hTexRef", "texture reference"),
int("dim", "dimension")
)
CUresult(
"TexRefGetFilterMode",
"""
Gets the filter-mode used by a texture reference. (Deprecated)
Returns in {@code *pfm} the filtering mode of the texture reference {@code hTexRef}.
""",
Check(1)..CUfilter_mode.p("pfm", "returned filtering mode"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetFormat",
"""
Gets the format used by a texture reference. (Deprecated)
Returns in {@code *pFormat} and {@code *pNumChannels} the format and number of components of the CUDA array bound to the texture reference
{@code hTexRef}. If {@code pFormat} or {@code pNumChannels} is #NULL, it will be ignored.
""",
Check(1)..CUarray_format.p("pFormat", "returned format"),
Check(1)..nullable..int.p("pNumChannels", "returned number of components"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetMipmapFilterMode",
"""
Gets the mipmap filtering mode for a texture reference. (Deprecated)
Returns the mipmap filtering mode in {@code pfm} that's used when reading memory through the texture reference {@code hTexRef}.
""",
Check(1)..CUfilter_mode.p("pfm", "returned mipmap filtering mode"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetMipmapLevelBias",
"""
Gets the mipmap level bias for a texture reference. (Deprecated)
Returns the mipmap level bias in {@code pBias} that's added to the specified mipmap level when reading memory through the texture reference {@code
hTexRef}.
""",
Check(1)..float.p("pbias", "returned mipmap level bias"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetMipmapLevelClamp",
"""
Gets the min/max mipmap level clamps for a texture reference. (Deprecated)
Returns the min/max mipmap level clamps in {@code pminMipmapLevelClamp} and {@code pmaxMipmapLevelClamp} that's used when reading memory through the
texture reference {@code hTexRef}.
""",
Check(1)..float.p("pminMipmapLevelClamp", "returned mipmap min level clamp"),
Check(1)..float.p("pmaxMipmapLevelClamp", "returned mipmap max level clamp"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetMaxAnisotropy",
"""
Gets the maximum anisotropy for a texture reference. (Deprecated)
Returns the maximum anisotropy in {@code pmaxAniso} that's used when reading memory through the texture reference {@code hTexRef}.
""",
Check(1)..int.p("pmaxAniso", "returned maximum anisotropy"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetBorderColor",
"""
Gets the border color used by a texture reference. (Deprecated)
Returns in {@code pBorderColor}, values of the RGBA color used by the texture reference {@code hTexRef}. The color value is of type float and holds
color components in the following sequence: pBorderColor[0] holds 'R' component pBorderColor[1] holds 'G' component pBorderColor[2] holds 'B' component
pBorderColor[3] holds 'A' component
""",
Check(4)..float.p("pBorderColor", "returned Type and Value of RGBA color"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefGetFlags",
"""
Gets the flags used by a texture reference. (Deprecated)
Returns in {@code *pFlags} the flags of the texture reference {@code hTexRef}.
""",
Check(1)..unsigned_int.p("pFlags", "returned flags"),
CUtexref("hTexRef", "texture reference")
)
CUresult(
"TexRefCreate",
"""
Creates a texture reference. (Deprecated)
Creates a texture reference and returns its handle in {@code *pTexRef}. Once created, the application must call #TexRefSetArray() or
#TexRefSetAddress() to associate the reference with allocated memory. Other texture reference functions are used to specify the format and
interpretation (addressing, filtering, etc.) to be used when the memory is read through this texture reference.
""",
Check(1)..CUtexref.p("pTexRef", "returned texture reference")
)
CUresult(
"TexRefDestroy",
"""
Destroys a texture reference. (Deprecated)
Destroys the texture reference specified by {@code hTexRef}.
""",
CUtexref("hTexRef", "texture reference to destroy")
)
CUresult(
"SurfRefSetArray",
"""
Sets the CUDA array for a surface reference.Deprecated:
Sets the CUDA array {@code hArray} to be read and written by the surface reference {@code hSurfRef}. Any previous CUDA array state associated with the
surface reference is superseded by this function. {@code Flags} must be set to 0. The #CUDA_ARRAY3D_SURFACE_LDST flag must have been set for the CUDA
array. Any CUDA array previously bound to {@code hSurfRef} is unbound.
""",
CUsurfref("hSurfRef", "surface reference handle"),
CUarray("hArray", "CUDA array handle"),
unsigned_int("Flags", "set to 0")
)
CUresult(
"SurfRefGetArray",
"""
Passes back the CUDA array bound to a surface reference. (Deprecated)
Returns in {@code *phArray} the CUDA array bound to the surface reference {@code hSurfRef}, or returns #CUDA_ERROR_INVALID_VALUE if the surface
reference is not bound to any CUDA array.
""",
Check(1)..CUarray.p("phArray", "surface reference handle"),
CUsurfref("hSurfRef", "surface reference handle")
)
IgnoreMissing..CUresult(
"TexObjectCreate",
"""
Creates a texture object.
Creates a texture object and returns it in {@code pTexObject}. {@code pResDesc} describes the data to texture from. {@code pTexDesc} describes how the
data should be sampled. {@code pResViewDesc} is an optional argument that specifies an alternate format for the data described by {@code pResDesc}, and
also describes the subresource region to restrict access to when texturing. {@code pResViewDesc} can only be specified if the type of resource is a
CUDA array or a CUDA mipmapped array.
Texture objects are only supported on devices of compute capability 3.0 or higher. Additionally, a texture object is an opaque value, and, as such,
should only be accessed through CUDA API calls.
${ul(
"""
If {@code CUDA_RESOURCE_DESC::resType} is set to #RESOURCE_TYPE_ARRAY, {@code CUDA_RESOURCE_DESC::res::array::hArray} must be set to a valid CUDA
array handle.
""",
"""
If {@code CUDA_RESOURCE_DESC::resType} is set to #RESOURCE_TYPE_MIPMAPPED_ARRAY, {@code CUDA_RESOURCE_DESC::res::mipmap::hMipmappedArray} must be
set to a valid CUDA mipmapped array handle.
""",
"""
If {@code CUDA_RESOURCE_DESC::resType} is set to #RESOURCE_TYPE_LINEAR, {@code CUDA_RESOURCE_DESC::res::linear::devPtr} must be set to a valid
device pointer, that is aligned to #DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT. {@code CUDA_RESOURCE_DESC::res::linear::format} and
{@code CUDA_RESOURCE_DESC::res::linear::numChannels} describe the format of each component and the number of components per array element.
{@code CUDA_RESOURCE_DESC::res::linear::sizeInBytes} specifies the size of the array in bytes. The total number of elements in the linear address
range cannot exceed #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE1D_LINEAR_WIDTH. The number of elements is computed as
{@code (sizeInBytes / (sizeof(format) * numChannels)).}
""",
"""
If {@code CUDA_RESOURCE_DESC::resType} is set to #RESOURCE_TYPE_PITCH2D, {@code CUDA_RESOURCE_DESC::res::pitch2D::devPtr} must be set to a valid
device pointer, that is aligned to #DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT. {@code CUDA_RESOURCE_DESC::res::pitch2D::format} and
{@code CUDA_RESOURCE_DESC::res::pitch2D::numChannels} describe the format of each component and the number of components per array element.
{@code CUDA_RESOURCE_DESC::res::pitch2D::width} and {@code CUDA_RESOURCE_DESC::res::pitch2D::height} specify the width and height of the array in
elements, and cannot exceed #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_WIDTH and #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_HEIGHT respectively.
{@code CUDA_RESOURCE_DESC::res::pitch2D::pitchInBytes} specifies the pitch between two rows in bytes and has to be aligned to
#DEVICE_ATTRIBUTE_TEXTURE_PITCH_ALIGNMENT. Pitch cannot exceed #DEVICE_ATTRIBUTE_MAXIMUM_TEXTURE2D_LINEAR_PITCH.
""",
"{@code flags} must be set to zero.",
)}
${ul(
"""
{@code CUDA_TEXTURE_DESC::addressMode} specifies the addressing mode for each dimension of the texture data. This is ignored if
{@code CUDA_RESOURCE_DESC::resType} is #RESOURCE_TYPE_LINEAR. Also, if the flag, #TRSF_NORMALIZED_COORDINATES is not set, the only supported
address mode is #TR_ADDRESS_MODE_CLAMP.
""",
"""
{@code CUDA_TEXTURE_DESC::filterMode} specifies the filtering mode to be used when fetching from the texture. This is ignored if
{@code CUDA_RESOURCE_DESC::resType} is #RESOURCE_TYPE_LINEAR.
""",
"""
{@code CUDA_TEXTURE_DESC::flags} can be any combination of the following:
${ul(
"""
#TRSF_READ_AS_INTEGER, which suppresses the default behavior of having the texture promote integer data to floating point data in the range [0,
1]. Note that texture with 32-bit integer format would not be promoted, regardless of whether or not this flag is specified.
""",
"""
#TRSF_NORMALIZED_COORDINATES, which suppresses the default behavior of having the texture coordinates range from [0, Dim) where Dim is the
width or height of the CUDA array. Instead, the texture coordinates [0, 1.0) reference the entire breadth of the array dimension; Note that for
CUDA mipmapped arrays, this flag has to be set.
""",
"""
#TRSF_DISABLE_TRILINEAR_OPTIMIZATION, which disables any trilinear filtering optimizations. Trilinear optimizations improve texture filtering
performance by allowing bilinear filtering on textures in scenarios where it can closely approximate the expected results.
"""
)}
""",
"""
{@code CUDA_TEXTURE_DESC::maxAnisotropy} specifies the maximum anisotropy ratio to be used when doing anisotropic filtering. This value will be
clamped to the range [1,16].
""",
"{@code CUDA_TEXTURE_DESC::mipmapFilterMode} specifies the filter mode when the calculated mipmap level lies between two defined mipmap levels.",
"{@code CUDA_TEXTURE_DESC::mipmapLevelBias} specifies the offset to be applied to the calculated mipmap level.",
"{@code CUDA_TEXTURE_DESC::minMipmapLevelClamp} specifies the lower end of the mipmap level range to clamp access to.",
"{@code CUDA_TEXTURE_DESC::maxMipmapLevelClamp} specifies the upper end of the mipmap level range to clamp access to."
)}
${ul(
"""
{@code CUDA_RESOURCE_VIEW_DESC::format} specifies how the data contained in the CUDA array or CUDA mipmapped array should be interpreted. Note that
this can incur a change in size of the texture data. If the resource view format is a block compressed format, then the underlying CUDA array or
CUDA mipmapped array has to have a base of format #AD_FORMAT_UNSIGNED_INT32. with 2 or 4 channels, depending on the block compressed format. For
ex., BC1 and BC4 require the underlying CUDA array to have a format of #AD_FORMAT_UNSIGNED_INT32 with 2 channels. The other BC formats require
the underlying resource to have the same base format but with 4 channels.
""",
"""
{@code CUDA_RESOURCE_VIEW_DESC::width} specifies the new width of the texture data. If the resource view format is a block compressed format, this
value has to be 4 times the original width of the resource. For non block compressed formats, this value has to be equal to that of the original
resource.
""",
"""
{@code CUDA_RESOURCE_VIEW_DESC::height} specifies the new height of the texture data. If the resource view format is a block compressed format,
this value has to be 4 times the original height of the resource. For non block compressed formats, this value has to be equal to that of the
original resource.
""",
"{@code CUDA_RESOURCE_VIEW_DESC::depth} specifies the new depth of the texture data. This value has to be equal to that of the original resource.",
"""
{@code CUDA_RESOURCE_VIEW_DESC::firstMipmapLevel} specifies the most detailed mipmap level. This will be the new mipmap level zero. For
non-mipmapped resources, this value has to be zero. {@code CUDA_TEXTURE_DESC::minMipmapLevelClamp} and
{@code CUDA_TEXTURE_DESC::maxMipmapLevelClamp} will be relative to this value. For ex., if the {@code firstMipmapLevel} is set to 2, and a
{@code minMipmapLevelClamp} of 1.2 is specified, then the actual minimum mipmap level clamp will be 3.2.
""",
"""
{@code CUDA_RESOURCE_VIEW_DESC::lastMipmapLevel} specifies the least detailed mipmap level. For non-mipmapped resources, this value has to be zero.
""",
"""
{@code CUDA_RESOURCE_VIEW_DESC::firstLayer} specifies the first layer index for layered textures. This will be the new layer zero. For non-layered
resources, this value has to be zero.
""",
"""
{@code CUDA_RESOURCE_VIEW_DESC::lastLayer} specifies the last layer index for layered textures. For non-layered resources, this value has to be
zero.
"""
)}
""",
Check(1)..CUtexObject.p("pTexObject", "texture object to create"),
CUDA_RESOURCE_DESC.const.p("pResDesc", "resource descriptor"),
CUDA_TEXTURE_DESC.const.p("pTexDesc", "texture descriptor"),
CUDA_RESOURCE_VIEW_DESC.const.p("pResViewDesc", "resource view descriptor")
)
IgnoreMissing..CUresult(
"TexObjectDestroy",
"""
Destroys a texture object.
Destroys the texture object specified by {@code texObject}.
""",
CUtexObject("texObject", "texture object to destroy")
)
IgnoreMissing..CUresult(
"TexObjectGetResourceDesc",
"""
Returns a texture object's resource descriptor.
Returns the resource descriptor for the texture object specified by {@code texObject}.
""",
CUDA_RESOURCE_DESC.p("pResDesc", "resource descriptor"),
CUtexObject("texObject", "texture object")
)
IgnoreMissing..CUresult(
"TexObjectGetTextureDesc",
"""
Returns a texture object's texture descriptor.
Returns the texture descriptor for the texture object specified by {@code texObject}.
""",
CUDA_TEXTURE_DESC.p("pTexDesc", "texture descriptor"),
CUtexObject("texObject", "texture object")
)
IgnoreMissing..CUresult(
"TexObjectGetResourceViewDesc",
"""
Returns a texture object's resource view descriptor.
Returns the resource view descriptor for the texture object specified by {@code texObject}. If no resource view was set for {@code texObject}, the
#CUDA_ERROR_INVALID_VALUE is returned.
""",
CUDA_RESOURCE_VIEW_DESC.p("pResViewDesc", "resource view descriptor"),
CUtexObject("texObject", "texture object")
)
IgnoreMissing..CUresult(
"SurfObjectCreate",
"""
Creates a surface object.
Creates a surface object and returns it in {@code pSurfObject}. {@code pResDesc} describes the data to perform surface load/stores on.
{@code CUDA_RESOURCE_DESC::resType} must be #RESOURCE_TYPE_ARRAY and {@code CUDA_RESOURCE_DESC::res::array::hArray} must be set to a valid CUDA array
handle. {@code CUDA_RESOURCE_DESC::flags} must be set to zero.
Surface objects are only supported on devices of compute capability 3.0 or higher. Additionally, a surface object is an opaque value, and, as such,
should only be accessed through CUDA API calls.
""",
Check(1)..CUsurfObject.p("pSurfObject", "surface object to create"),
CUDA_RESOURCE_DESC.const.p("pResDesc", "resource descriptor")
)
IgnoreMissing..CUresult(
"SurfObjectDestroy",
"""
Destroys a surface object.
Destroys the surface object specified by {@code surfObject}.
""",
CUsurfObject("surfObject", "surface object to destroy")
)
IgnoreMissing..CUresult(
"SurfObjectGetResourceDesc",
"""
Returns a surface object's resource descriptor.
Returns the resource descriptor for the surface object specified by {@code surfObject}.
""",
CUDA_RESOURCE_DESC.p("pResDesc", "resource descriptor"),
CUsurfObject("surfObject", "surface object")
)
IgnoreMissing..CUresult(
"DeviceCanAccessPeer",
"""
Queries if a device may directly access a peer device's memory.
Returns in {@code *canAccessPeer} a value of 1 if contexts on {@code dev} are capable of directly accessing memory from contexts on {@code peerDev} and
0 otherwise. If direct access of {@code peerDev} from {@code dev} is possible, then access may be enabled on two specific contexts by calling
#CtxEnablePeerAccess().
""",
Check(1)..int.p("canAccessPeer", "returned access capability"),
CUdevice("dev", "device from which allocations on {@code peerDev} are to be directly accessed"),
CUdevice("peerDev", "device on which the allocations to be directly accessed by {@code dev} reside")
)
IgnoreMissing..CUresult(
"CtxEnablePeerAccess",
"""
Enables direct access to memory allocations in a peer context.
If both the current context and {@code peerContext} are on devices which support unified addressing (as may be queried using
#DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING) and same major compute capability, then on success all allocations from {@code peerContext} will immediately
be accessible by the current context. See ref for additional details.
Note that access granted by this call is unidirectional and that in order to access memory from the current context in {@code peerContext}, a separate
symmetric call to #CtxEnablePeerAccess() is required.
Note that there are both device-wide and system-wide limitations per system configuration, as noted in the CUDA Programming Guide under the section
"Peer-to-Peer Memory Access".
Returns #CUDA_ERROR_PEER_ACCESS_UNSUPPORTED if #DeviceCanAccessPeer() indicates that the {@code CUdevice} of the current context cannot directly access
memory from the {@code CUdevice} of {@code peerContext}.
Returns #CUDA_ERROR_PEER_ACCESS_ALREADY_ENABLED if direct access of {@code peerContext} from the current context has already been enabled.
Returns #CUDA_ERROR_TOO_MANY_PEERS if direct peer access is not possible because hardware resources required for peer access have been exhausted.
Returns #CUDA_ERROR_INVALID_CONTEXT if there is no current context, {@code peerContext} is not a valid context, or if the current context is {@code
peerContext}.
Returns #CUDA_ERROR_INVALID_VALUE if {@code Flags} is not 0.
""",
CUcontext("peerContext", "peer context to enable direct access to from the current context"),
unsigned_int("Flags", "reserved for future use and must be set to 0")
)
IgnoreMissing..CUresult(
"CtxDisablePeerAccess",
"""
Disables direct access to memory allocations in a peer context and unregisters any registered allocations.
Returns #CUDA_ERROR_PEER_ACCESS_NOT_ENABLED if direct peer access has not yet been enabled from {@code peerContext} to the current context.
Returns #CUDA_ERROR_INVALID_CONTEXT if there is no current context, or if {@code peerContext} is not a valid context.
""",
CUcontext("peerContext", "peer context to disable direct access to")
)
IgnoreMissing..CUresult(
"DeviceGetP2PAttribute",
"""
Queries attributes of the link between two devices.
Returns in {@code *value} the value of the requested attribute {@code attrib} of the link between {@code srcDevice} and {@code dstDevice}. The
supported attributes are:
${ul(
"#DEVICE_P2P_ATTRIBUTE_PERFORMANCE_RANK: A relative value indicating the performance of the link between two devices.",
"#DEVICE_P2P_ATTRIBUTE_ACCESS_SUPPORTED P2P: 1 if P2P Access is enable.",
"#DEVICE_P2P_ATTRIBUTE_NATIVE_ATOMIC_SUPPORTED: 1 if Atomic operations over the link are supported.",
"#DEVICE_P2P_ATTRIBUTE_CUDA_ARRAY_ACCESS_SUPPORTED: 1 if cudaArray can be accessed over the link."
)}
Returns #CUDA_ERROR_INVALID_DEVICE if {@code srcDevice} or {@code dstDevice} are not valid or if they represent the same device.
Returns #CUDA_ERROR_INVALID_VALUE if {@code attrib} is not valid or if {@code value} is a null pointer.
""",
Check(1)..int.p("value", "returned value of the requested attribute"),
CUdevice_P2PAttribute("attrib", "the requested attribute of the link between {@code srcDevice} and {@code dstDevice}"),
CUdevice("srcDevice", "the source device of the target link"),
CUdevice("dstDevice", "the destination device of the target link")
)
CUresult(
"GraphicsUnregisterResource",
"""
Unregisters a graphics resource for access by CUDA.
Unregisters the graphics resource {@code resource} so it is not accessible by CUDA unless registered again.
If {@code resource} is invalid then #CUDA_ERROR_INVALID_HANDLE is returned.
""",
CUgraphicsResource("resource", "resource to unregister")
)
CUresult(
"GraphicsSubResourceGetMappedArray",
"""
Get an array through which to access a subresource of a mapped graphics resource.
Returns in {@code *pArray} an array through which the subresource of the mapped graphics resource {@code resource} which corresponds to array index
{@code arrayIndex} and mipmap level {@code mipLevel} may be accessed. The value set in {@code *pArray} may change every time that {@code resource} is
mapped.
If {@code resource} is not a texture then it cannot be accessed via an array and #CUDA_ERROR_NOT_MAPPED_AS_ARRAY is returned. If {@code arrayIndex} is
not a valid array index for {@code resource} then #CUDA_ERROR_INVALID_VALUE is returned. If {@code mipLevel} is not a valid mipmap level for {@code
resource} then #CUDA_ERROR_INVALID_VALUE is returned. If {@code resource} is not mapped then #CUDA_ERROR_NOT_MAPPED is returned.
""",
Check(1)..CUarray.p("pArray", "returned array through which a subresource of {@code resource} may be accessed"),
CUgraphicsResource("resource", "mapped resource to access"),
unsigned_int(
"arrayIndex",
"array index for array textures or cubemap face index as defined by {@code CUarray_cubemap_face} for cubemap textures for the subresource to access"
),
unsigned_int("mipLevel", "mipmap level for the subresource to access")
)
IgnoreMissing..CUresult(
"GraphicsResourceGetMappedMipmappedArray",
"""
Get a mipmapped array through which to access a mapped graphics resource.
Returns in {@code *pMipmappedArray} a mipmapped array through which the mapped graphics resource {@code resource}. The value set in
{@code *pMipmappedArray} may change every time that {@code resource} is mapped.
If {@code resource} is not a texture then it cannot be accessed via a mipmapped array and #CUDA_ERROR_NOT_MAPPED_AS_ARRAY is returned. If {@code
resource} is not mapped then #CUDA_ERROR_NOT_MAPPED is returned.
""",
Check(1)..CUmipmappedArray.p("pMipmappedArray", "returned mipmapped array through which {@code resource} may be accessed"),
CUgraphicsResource("resource", "mapped resource to access")
)
CUresult(
"GraphicsResourceGetMappedPointer",
"""
Get a device pointer through which to access a mapped graphics resource.
Returns in {@code *pDevPtr} a pointer through which the mapped graphics resource {@code resource} may be accessed. Returns in {@code pSize} the size of
the memory in bytes which may be accessed from that pointer. The value set in {@code pPointer} may change every time that {@code resource} is mapped.
If {@code resource} is not a buffer then it cannot be accessed via a pointer and #CUDA_ERROR_NOT_MAPPED_AS_POINTER is returned. If {@code resource} is
not mapped then #CUDA_ERROR_NOT_MAPPED is returned. *
""",
Check(1)..CUdeviceptr.p("pDevPtr", "returned pointer through which {@code resource} may be accessed"),
Check(1)..size_t.p("pSize", "returned size of the buffer accessible starting at {@code *pPointer}"),
CUgraphicsResource("resource", "mapped resource to access")
).versioned()
CUresult(
"GraphicsResourceSetMapFlags",
"""
Set usage flags for mapping a graphics resource.
Set {@code flags} for mapping the graphics resource {@code resource}.
Changes to {@code flags} will take effect the next time {@code resource} is mapped. The {@code flags} argument may be any of the following:
${ul(
"""
#GRAPHICS_MAP_RESOURCE_FLAGS_NONE: Specifies no hints about how this resource will be used. It is therefore assumed that this resource will be
read from and written to by CUDA kernels. This is the default value.
""",
"#GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY: Specifies that CUDA kernels which access this resource will not write to this resource.",
"""
#GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD: Specifies that CUDA kernels which access this resource will not read from this resource and will
write over the entire contents of the resource, so none of the data previously stored in the resource will be preserved.
"""
)}
If {@code resource} is presently mapped for access by CUDA then #CUDA_ERROR_ALREADY_MAPPED is returned. If {@code flags} is not one of the above
values then #CUDA_ERROR_INVALID_VALUE is returned.
""",
CUgraphicsResource("resource", "registered resource to set flags for"),
unsigned_int("flags", "parameters for resource mapping")
).versioned()
CUresult(
"GraphicsMapResources",
"""
Map graphics resources for access by CUDA.
Maps the {@code count} graphics resources in {@code resources} for access by CUDA.
The resources in {@code resources} may be accessed by CUDA until they are unmapped. The graphics API from which {@code resources} were registered
should not access any resources while they are mapped by CUDA. If an application does so, the results are undefined.
This function provides the synchronization guarantee that any graphics calls issued before #GraphicsMapResources() will complete before any
subsequent CUDA work issued in {@code stream} begins.
If {@code resources} includes any duplicate entries then #CUDA_ERROR_INVALID_HANDLE is returned. If any of {@code resources} are presently mapped for
access by CUDA then #CUDA_ERROR_ALREADY_MAPPED is returned.
""",
AutoSize("resources")..unsigned_int("count", "number of resources to map"),
CUgraphicsResource.p("resources", "resources to map for CUDA usage"),
nullable..CUstream("hStream", "stream with which to synchronize")
).ptsz()
CUresult(
"GraphicsUnmapResources",
"""
Unmap graphics resources.
Unmaps the {@code count} graphics resources in {@code resources}.
Once unmapped, the resources in {@code resources} may not be accessed by CUDA until they are mapped again.
This function provides the synchronization guarantee that any CUDA work issued in {@code stream} before #GraphicsUnmapResources() will complete
before any subsequently issued graphics work begins.
If {@code resources} includes any duplicate entries then #CUDA_ERROR_INVALID_HANDLE is returned. If any of {@code resources} are not presently mapped
for access by CUDA then #CUDA_ERROR_NOT_MAPPED is returned.
""",
AutoSize("resources")..unsigned_int("count", "number of resources to unmap"),
CUgraphicsResource.p("resources", "resources to unmap"),
nullable..CUstream("hStream", "stream with which to synchronize")
).ptsz()
IgnoreMissing..CUresult(
"GetProcAddress",
"""
Returns the requested driver API function pointer.
Returns in {@code **pfn} the address of the CUDA driver function for the requested CUDA version and flags.
The CUDA version is specified as (1000 * major + 10 * minor), so CUDA 11.2 should be specified as 11020. For a requested driver symbol, if the
specified CUDA version is greater than or equal to the CUDA version in which the driver symbol was introduced, this API will return the function
pointer to the corresponding versioned function.
The pointer returned by the API should be cast to a function pointer matching the requested driver function's definition in the API header file. The
function pointer typedef can be picked up from the corresponding typedefs header file. For example, cudaTypedefs.h consists of function pointer
typedefs for driver APIs defined in cuda.h.
The API will return #CUDA_ERROR_NOT_FOUND if the requested driver function is not supported on the platform, no ABI compatible driver function exists
for the specified {@code cudaVersion} or if the driver symbol is invalid.
The requested flags can be:
${ul(
"""
#GET_PROC_ADDRESS_DEFAULT: This is the default mode. This is equivalent to #GET_PROC_ADDRESS_PER_THREAD_DEFAULT_STREAM if the code is
compiled with --default-stream per-thread compilation flag or the macro {@code CUDA_API_PER_THREAD_DEFAULT_STREAM} is defined;
#GET_PROC_ADDRESS_LEGACY_STREAM otherwise.
""",
"""
#GET_PROC_ADDRESS_LEGACY_STREAM: This will enable the search for all driver symbols that match the requested driver symbol name except the
corresponding per-thread versions.
""",
"""
#GET_PROC_ADDRESS_PER_THREAD_DEFAULT_STREAM: This will enable the search for all driver symbols that match the requested driver symbol name
including the per-thread versions. If a per-thread version is not found, the API will return the legacy version of the driver function.
"""
)}
""",
charASCII.const.p(
"symbol",
"""
the base name of the driver API function to look for. As an example, for the driver API {@code cuMemAlloc_v2()}, {@code symbol} would be
{@code cuMemAlloc} and {@code cudaVersion} would be the ABI compatible CUDA version for the {@code _v2} variant.
"""
),
Check(1)..void.p.p("pfn", "location to return the function pointer to the requested driver function"),
int("cudaVersion", "the CUDA version to look for the requested driver symbol"),
cuuint64_t("flags", "flags to specify search options")
)
CUresult(
"GetExportTable",
"",
Unsafe..void.const.p.p("ppExportTable", ""),
CUuuid.const.p("pExportTableId", "")
)
} | bsd-3-clause | 10d024c860ba0cc51c6daa865375567e | 53.842934 | 245 | 0.672317 | 4.533255 | false | false | false | false |
vovagrechka/fucking-everything | alraune/alraune/src/main/java/alraune/comparison.kt | 1 | 14509 | package alraune
import alraune.entity.*
import org.apache.commons.collections4.Equator
import org.apache.commons.collections4.sequence.CommandVisitor
import org.apache.commons.collections4.sequence.SequencesComparator
import pieces100.*
import alraune.AlTag.*
import bolone.sion
import vgrechka.*
import java.sql.Timestamp
import kotlin.reflect.KMutableProperty0
import kotlin.reflect.KProperty1
import kotlin.reflect.full.memberProperties
class PropComparisonParams<T, R>(
val prop: KProperty1<T, R>,
val propNamePrefix: String = "",
val presentLong: (Long) -> String = {it.toString()})
object PropComparisonPile {
object NAValue
val css = AlCSS.historyTab
fun text() = Context1.maybe()?.propComparison?.text
fun composePropName(name: String) =
span().className(css.propName).text(name)
fun composeKindaBlank(text: String) =
span().style("font-style: italic; color: ${Color.Gray500};")
.add(text)
fun beforeTitle() = text()?.before ?: t("TOTE", "Было")
fun afterTitle() = text()?.after ?: t("TOTE", "Стало")
fun composeChangeScriptPieces(value1: String, value2: String, includeInsertions: Boolean, includeDeletions: Boolean): AlTag {
class Word(val text: String, val numNewlines: Int)
fun findWords(haystack: String): List<Word> {
val reWord = Regex("""\S+""")
val list = mutableListOf<Word>()
var start = 0
while (true) {
val mr = reWord.find(haystack, start) ?: break
list += Word(text = mr.value, numNewlines = (start until mr.range.first).count {haystack[it] == '\n'})
start = mr.range.last + 1
}
return list
}
val words1 = findWords(value1)
val words2 = findWords(value2)
val comp = SequencesComparator(words1, words2, object : Equator<Word> {
override fun equate(o1: Word, o2: Word) = o1.text == o2.text
override fun hash(o: Word) = o.text.hashCode()
})
fun addPiece(word: Word, style: String) {
oo(span().style(style).add("\n".repeat(word.numNewlines) + word.text))
oo(span(" "))
}
return div().style("white-space: pre-wrap;").with {
comp.script.visit(object : CommandVisitor<Word> {
override fun visitInsertCommand(text: Word) {
if (includeInsertions)
addPiece(text, "background: ${Color.Green100};")
}
override fun visitDeleteCommand(text: Word) {
if (includeDeletions)
addPiece(text, "background: ${Color.Red100}; text-decoration: line-through;")
}
override fun visitKeepCommand(text: Word) {
addPiece(text, "")
}
})
}
}
fun shitToHuman(shit: Any?, presentLong: (Long) -> String = {it.toString()}): Renderable {
if (shit === NAValue) return composeKindaBlank("<N/A>")
if (shit == null) return composeKindaBlank("<null>")
if (shit is String) return when {
shit.isEmpty() -> composeKindaBlank("<empty>")
shit.isBlank() -> composeKindaBlank("<blank>")
else -> composePreWrapSpan(shit)
}
if (shit is Long) return span(presentLong(shit))
if (shit is Int) return span("" + shit)
if (shit is Boolean) return span("" + shit)
if (shit is Enum<*>) return span(shit.name)
if (shit is Timestamp) return span(TimePile.kievTimeString(shit))
if (shit is JsonizableLocalDate) return span(shit.format())
wtf()
}
fun ooSingleValue(propName: String, value: Any?) {
val rowDivDomid by nextJsIdentDel()
val rowDiv = div().id(rowDivDomid).classes(css.row, css.flexRow)
val view = TitledView("Single", rowDiv)
oo(rowDiv.with {
oo(div().with {
oo(PropComparisonPile.composePropName(propName))
oo(PropComparisonPile.shitToHuman(value))
})
})
}
}
object PropComparisonPile2 {
fun valueOrNa(obj: Any?, prop: KProperty1<Experience, Any?>) = when {
obj != null -> prop.getAnyQuestion(obj)
else -> PropComparisonPile.NAValue
}
}
class PropComparisonShit<T, R>(val p: PropComparisonParams<T, R>) {
val views = mutableListOf<TitledView>()
val css = PropComparisonPile.css
fun specificPropComposer(): ((Any?) -> Renderable)? {
if (p.prop == AlUAOrder::documentCategory || p.prop == Order.Data::documentCategory)
return {span(longDocumentCategoryTitle(it as String))}
if (p.prop == Order.Resource::size)
return {span(formatFileSizeApprox(rctx.locale, it as Int))}
return null
}
fun shitToHuman(shit: Any?): Renderable {
specificPropComposer()?.let {return it(shit)}
return PropComparisonPile.shitToHuman(shit, p.presentLong)
}
fun ooSingleValue(value: Any?) {
val rowDivDomid by nextJsIdentDel()
val rowDiv = div().id(rowDivDomid).classes(css.row, css.flexRow)
val view = TitledView("Single", rowDiv)
views += view
oo(rowDiv.with {
oo(div().with {
ooPropName()
oo(shitToHuman(value))
})
})
}
fun ooPropName(): AlTag = PropComparisonPile.composePropName(prefixedPropName()).also {oo(it)}
fun prefixedPropName() = p.propNamePrefix + p.prop.name
}
class OoPropValueComparison<T, R>(val p: PropComparisonParams<T, R>, val value1: Any?, val value2: Any?) : DancerBase<Unit>() {
val css = AlCSS.historyTab
val css2 = AlCSS.historyTab.switchView
val pcs = PropComparisonShit(p)
override fun dance() {
if (value1 == value2) return ooValue2Unchanged()
pcs.specificPropComposer()?.let {return drooTransformArrow(it)}
if (value1 is String && value1.isNotBlank()
&& value2 is String && value2.isNotBlank())
return !drooTextualDiffsAndStuff(value1, value2)
drooTransformArrow(pcs::shitToHuman)
}
fun ooValue2Unchanged() {
pcs.ooSingleValue(value2)
}
fun drooTransformArrow(block: (Any?) -> Renderable) {
drooArrow(block(value1), block(value2))
}
fun drooArrow(left: Renderable, right: Renderable) {
oo(div().classes(css.row, css.flexRow, css.changedRow1).with {
pcs.views += TitledView("FromTo", currentTag())
oo(div().with {
pcs.ooPropName().let {
if (value2 === PropComparisonPile.NAValue)
it.appendStyle("text-decoration: line-through;")
}
oo(left)
})
oo(FA.arrowCircleRight().appendClassName(css.p5))
oo(right)
})
}
inner class drooTextualDiffsAndStuff(val value1: String, val value2: String): DancerBase<Unit>() {
override fun dance() {
!OoSwitcher(sectionTitle = pcs.prefixedPropName(), items = !makeTextualComparisonFuckers(value1, value2))
}
}
}
fun <T, R> ooPropComparison(p: PropComparisonParams<T, R>, obj1: Any?, obj2: Any) {
val value2 = p.prop.getAnyQuestion(obj2)
if (obj1 == null) return PropComparisonShit(p).ooSingleValue(value2)
val value1 = p.prop.getAnyQuestion(obj1)
!OoPropValueComparison(p, value1, value2)
}
class makeTextualComparisonFuckers(val value1: String, val value2: String) : DancerBase<List<TitledView>>() {
val list = mutableListOf<TitledView>()
val css = AlCSS.historyTab
override fun dance(): MutableList<TitledView> {
addInlineDiffView()
addFromToWithChangesView()
addBefore()
addAfter()
return list
}
fun addInlineDiffView() {
list += TitledView(t("TOTE", "Правки вместе"), composeChangeScriptPieces(includeInsertions = true, includeDeletions = true))
}
fun addFromToWithChangesView() {
list += TitledView(t("TOTE", "Правки раздельно"), div().className(css.flexRow).with {
oo(composeChangeScriptPieces(includeInsertions = false, includeDeletions = true))
oo(FA.arrowCircleRight().appendClassName(css.p5))
oo(composeChangeScriptPieces(includeInsertions = true, includeDeletions = false))
})
}
fun addBefore() {
list += TitledView(PropComparisonPile.beforeTitle(), div().style("white-space: pre-wrap;").add(value1))
}
fun addAfter() {
list += TitledView(PropComparisonPile.afterTitle(), div().style("white-space: pre-wrap;").add(value2))
}
fun composeChangeScriptPieces(includeInsertions: Boolean, includeDeletions: Boolean) =
PropComparisonPile.composeChangeScriptPieces(value1, value2, includeInsertions, includeDeletions)
}
class OoPropComparisons<T : Any>(val shit1: T?, val shit2: T, exclude: Set<KProperty1<T, *>>, block: (OoPropComparisons<T>) -> Unit) {
val exclude = exclude.toMutableSet()
init {
oo(div().with { // Children are counted in CSS (nth-child stuff) relative to this div
block(this)
})
}
fun <R> prop(pcp: PropComparisonParams<T, R>) {
if (!exclude.contains(pcp.prop)) {
exclude += pcp.prop
ooPropComparison(pcp, shit1, shit2)
}
}
fun <R> prop(prop: KProperty1<T, R>) {
prop(PropComparisonParams(prop))
}
fun compound(prop: KProperty1<T, *>) {
if (!exclude.contains(prop)) {
exclude += prop
fun ooJustNullForCompoundPropAsWhole() =
PropComparisonShit(PropComparisonParams(prop))
.ooSingleValue(null)
fun forSubProps(block: (PropComparisonParams<*, *>) -> Unit) =
prop.returnClass.memberProperties.forEach {
block(PropComparisonParams(it, propNamePrefix = "${prop.name}."))
}
val compoundValue2 = prop.getAnyQuestion(shit2)
if (shit1 == null) {
if (compoundValue2 == null)
ooJustNullForCompoundPropAsWhole()
else
forSubProps {
PropComparisonShit(it).ooSingleValue(it.prop.getAnyQuestion(compoundValue2))}
}
else {
val compoundValue1 = prop.getAnyQuestion(shit1)
if (compoundValue1 == null && compoundValue2 == null)
ooJustNullForCompoundPropAsWhole()
else
forSubProps {pcp->
val first = when {
compoundValue1 == null -> PropComparisonPile.NAValue
else -> pcp.prop.getAnyQuestion(compoundValue1)
}
val second = when {
compoundValue2 == null -> PropComparisonPile.NAValue
else -> pcp.prop.getAnyQuestion(compoundValue2)
}
!OoPropValueComparison(pcp, first, second)
}
}
}
}
fun restProps() {
for (prop in shit2::class.memberProperties) {
this.prop(prop as KProperty1<T, *>)
}
}
}
fun ooOrderUserNoteComparison(note1: Order.UserNote?, note2: Order.UserNote) {
Order.UserNote.__version1
fun ooNoteProps(vararg ps: (Order.UserNote) -> KMutableProperty0<*>) {
for (p in ps) {
ooPropComparison(PropComparisonParams(
prop0To1(Order.UserNote::class, p(note2)), propNamePrefix = ""), note1, note2)
}
}
ooNoteProps({it::text})
}
fun ooOrderParamsComparison_a2__killme(order1: Order?, order2: Order) {
// Order.__version2
fun ooOrderProps(vararg ps: (Order) -> KMutableProperty0<*>) {
for (p in ps) {
ooPropComparison(PropComparisonParams(
prop0To1(Order::class, p(order2)), propNamePrefix = ""), order1, order2)
}
}
fun ooDataProps(vararg ps: (Order.Data) -> KMutableProperty0<*>) {
for (p in ps) {
ooPropComparison(PropComparisonParams(
prop0To1(Order.Data::class, p(order2.data)), propNamePrefix = ""), order1?.data, order2.data)
}
}
// ooOrderProps({it::state}, {it::deadline})
// imf("buckets")
// ooDataProps({it::createdAt}, {it::updatedAt}, {it::email}, {it::contactName}, {it::phone}, {it::documentType},
// {it::documentTitle}, {it::documentDetails}, {it::documentCategory}, {it::numPages}, {it::numSources},
// {it::adminNotes}, {it::lastOperationId}, {it::approvalTaskId}, {it::lastRejectedByOperationId},
// {it::rejectionReason}, {it::wasRejectionReason})
//
//
// OrderFileComparison(order2.data.id).ooFileSection(
// filesBefore = order1?.data?.files ?: listOf(),
// filesAfter = order2.data.files)
// TODO:vgrechka Bidding
// TODO:vgrechka Assignment
ooPropComparison(PropComparisonParams(Order::state), order1, order2)
ooPropComparison(PropComparisonParams(Order::deadline), order1, order2)
}
//fun ooOrderParamsComparison_a2_killme(shit1: Order_UA_V1?, shit2: Order_UA_V1) {
// OoPropComparisons(shit1, shit2,
// exclude = setOf(Order_UA_V1::files)) {
// it.prop(PropComparisonParams(Order_UA_V1::id, presentLong = {AlText.orderId(it)}))
// it.prop(Order_UA_V1::uuid)
// it.prop(Order_UA_V1::createdAt)
// it.prop(Order_UA_V1::updatedAt)
// it.prop(Order_UA_V1::deadline)
// it.prop(Order_UA_V1::state)
// it.prop(Order_UA_V1::contactName)
// it.prop(Order_UA_V1::phone)
// it.prop(Order_UA_V1::documentTitle)
// it.prop(Order_UA_V1::documentDetails)
// it.restProps()
// }
//}
fun ooOrderFileComparison(shit1: Order.File?, shit2: Order.File) {
ver(sion.OrderFileFields__1)
oo(div().classes(AlCSS.childrenSpaced5px).with {
ooPropComparison(PropComparisonParams(Order.File::title), shit1, shit2)
ooPropComparison(PropComparisonParams(Order.File::details), shit1, shit2)
ooPropComparison(PropComparisonParams(Order.Resource::name), shit1?.resource, shit2.resource)
ooPropComparison(PropComparisonParams(Order.Resource::size), shit1?.resource, shit2.resource)
})
}
| apache-2.0 | 8d05de6a6b0da5a180754fe77b967ce1 | 33.296209 | 134 | 0.601396 | 3.886412 | false | false | false | false |
dkandalov/katas | kotlin/src/katas/kotlin/leetcode/super_egg_drop/SuperEggDrop.kt | 1 | 3360 | package katas.kotlin.leetcode.super_egg_drop
import datsok.shouldEqual
import org.junit.jupiter.api.Test
// https://leetcode.com/problems/super-egg-drop ❌
// https://www.youtube.com/watch?v=xsOCvSiSrSs
//
// You are given K eggs, and you have access to a building with N floors from 1 to N.
// Each egg is identical in function, and if an egg breaks, you cannot drop it again.
// You know that there exists a floor F with 0 <= F <= N such that any egg dropped at a floor
// higher than F will break, and any egg dropped at or below floor F will not break.
// Your goal is to know with certainty what the value of F is.
//
// Each move, you may take an egg (if you have an unbroken one) and drop it from any floor X (with 1 <= X <= N).
// What is the minimum number of moves that you need to know with certainty what F is,
// regardless of the initial value of F?
//
// Note:
// 1 <= K <= 100
// 1 <= N <= 10000
//
// Example 1:
// Input: K = 1, N = 2
// Output: 2
// Explanation:
// Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
// Otherwise, drop the egg from floor 2. If it breaks, we know with certainty that F = 1.
// If it didn't break, then we know with certainty F = 2.
// Hence, we needed 2 moves in the worst case to know what F is with certainty.
//
// Example 2:
// Input: K = 2, N = 6
// Output: 3
//
// Example 3:
// Input: K = 3, N = 14
// Output: 4
// 0 1 2
// 0 1 2 3
// 0 1 2 3 4
// 0 1 2 3 4 5
// 0 1 2 3 4 5 6
// 0 1 2 3 4 5 6 7
// 0 1 2 3 4 5 6 7 8 9
// 0 1 2 3 4 5 6 7 8 9 10 11 12
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
fun superEggDrop(eggs: Int, floors: Int): Int {
require(eggs >= 1 && floors >= 0)
if (floors == 0) return 0
if (eggs == 1) return floors
val midFloor = when (floors) {
12 -> (floors / 2) - 1
9 -> floors / 2
else -> (floors + 1) / 2
}
val stepsBelow = superEggDrop(eggs - 1, floors = midFloor - 1) // broken egg
val stepsAbove = superEggDrop(eggs, floors = floors - midFloor) // egg didn't break
return maxOf(stepsBelow, stepsAbove) + 1
}
class Tests {
@Test fun examples() {
superEggDrop(eggs = 1, floors = 0) shouldEqual 0
superEggDrop(eggs = 1, floors = 1) shouldEqual 1
superEggDrop(eggs = 1, floors = 2) shouldEqual 2
superEggDrop(eggs = 2, floors = 2) shouldEqual 2
superEggDrop(eggs = 1, floors = 3) shouldEqual 3
superEggDrop(eggs = 2, floors = 3) shouldEqual 2
superEggDrop(eggs = 1, floors = 4) shouldEqual 4
superEggDrop(eggs = 2, floors = 4) shouldEqual 3
superEggDrop(eggs = 1, floors = 5) shouldEqual 5
superEggDrop(eggs = 2, floors = 5) shouldEqual 3
superEggDrop(eggs = 1, floors = 6) shouldEqual 6
superEggDrop(eggs = 2, floors = 6) shouldEqual 3
superEggDrop(eggs = 3, floors = 6) shouldEqual 3
superEggDrop(eggs = 2, floors = 9) shouldEqual 4
superEggDrop(eggs = 3, floors = 9) shouldEqual 4
superEggDrop(eggs = 2, floors = 12) shouldEqual 5
superEggDrop(eggs = 3, floors = 12) shouldEqual 4
superEggDrop(eggs = 2, floors = 14) shouldEqual 7
superEggDrop(eggs = 3, floors = 14) shouldEqual 4
// TODO superEggDrop(eggs = 3, floors = 25) shouldEqual 5
superEggDrop(eggs = 100, floors = 10_000) shouldEqual 14
}
} | unlicense | c1a3d94d5e4d8c4e50600acc162d366f | 32.59 | 112 | 0.62835 | 3.364729 | false | false | false | false |
martijn-heil/wac-core | src/main/kotlin/tk/martijn_heil/wac_core/general/itemproperty/ItemPropertyListener.kt | 1 | 11993 | /*
* wac-core
* Copyright (C) 2016 Martijn Heil
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package tk.martijn_heil.wac_core.general.itemproperty;
import isSoulBound
import isUnbreakable
import org.bukkit.ChatColor
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
import org.bukkit.event.entity.PlayerDeathEvent
import org.bukkit.event.inventory.*
import org.bukkit.event.player.PlayerArmorStandManipulateEvent
import org.bukkit.event.player.PlayerDropItemEvent
import org.bukkit.event.player.PlayerInteractEntityEvent
import org.bukkit.event.player.PlayerItemDamageEvent
import tk.martijn_heil.wac_core.WacCore
class ItemPropertyListener : Listener {
@EventHandler(priority = EventPriority.HIGHEST) // If player tries to put a soulbound item in an item frame..
fun onPlayerInteractEntity(e: PlayerInteractEntityEvent) {
if (e.rightClicked.type == EntityType.ITEM_FRAME && isSoulBound(
e.player.inventory.itemInMainHand)) {
e.isCancelled = true;
e.player.sendMessage(WacCore.messages.getString("error.event.cancelled.entity.itemFrame.putItemIn"));
}
}
@EventHandler(priority = EventPriority.HIGHEST) // If player tries to drop a soulbound item..
fun onPlayerDropItem(e: PlayerDropItemEvent) {
// If dropped item is soulbound, cancel the event.
if (isSoulBound(e.itemDrop.itemStack)) {
e.isCancelled = true;
e.player.updateInventory();
e.player.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("error.event.cancelled.item.drop"));
}
}
@EventHandler(priority = EventPriority.HIGHEST) // If player tries to put a soulbound item in another inventory..
fun onInventoryClick(e: InventoryClickEvent) {
if(e.whoClicked !is Player) return
if (e.currentItem != null && e.action == InventoryAction.MOVE_TO_OTHER_INVENTORY &&
isSoulBound(e.currentItem) &&
e.inventory.type != InventoryType.PLAYER &&
e.inventory.type != InventoryType.CRAFTING &&
e.inventory.type != InventoryType.CREATIVE) {
e.isCancelled = true
e.whoClicked.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("error.event.cancelled.inventory.putItemIn"));
}
// Click the item, and then click it into the slot in the chest
val clicked = e.inventory
if (clicked == null || clicked.type != InventoryType.PLAYER &&
clicked.type != InventoryType.CREATIVE &&
clicked.type != InventoryType.CRAFTING) {
// The cursor item is going into the top inventory
val onCursor = e.cursor
if (onCursor != null && isSoulBound(onCursor)) {
e.isCancelled = true
// If player tries to drop item by clicking outside of his inventory while dragging the item..
// the PlayerDropItemEvent would cancel this aswell, but this keeps the item being dragged,
// The PlayerDropItemEvent just puts the item back into the inventory, so this is a bit nicer..
if (clicked == null) {
e.whoClicked.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("error.event.cancelled.item.drop"))
} else {
e.whoClicked.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("error.event.cancelled.inventory.putItemIn"))
}
}
}
if (e.currentItem != null && e.inventory != null) {
var cancel = false
for (i in e.inventory.contents) {
if (i != null && isSoulBound(i)) {
cancel = true
break
}
}
// If clicked inventory is a WORKBENCH or CRAFTING inventory
if (e.inventory.type == InventoryType.WORKBENCH || e.inventory.type == InventoryType.CRAFTING) {
if (e.slotType == InventoryType.SlotType.RESULT && cancel) {
e.isCancelled = true;
e.whoClicked.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("event.error.cancelled.item.craft.soulbound"));
}
}
}
if (e.currentItem != null && isSoulBound(e.currentItem) &&
(e.click == ClickType.DROP || e.click == ClickType.CONTROL_DROP)) {
e.isCancelled = true
(e.whoClicked as Player).updateInventory()
e.whoClicked.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("error.event.cancelled.item.drop"));
}
// else if(ItemStacks.isSoulBound(chunkPropagateSkylightOcclusion.getCursor()) && chunkPropagateSkylightOcclusion.getSlotType() == InventoryType.SlotType.OUTSIDE &&
// chunkPropagateSkylightOcclusion.getClickedInventory() == null)
// {
// chunkPropagateSkylightOcclusion.setCancelled(true);
// ((Player) chunkPropagateSkylightOcclusion.getWhoClicked()).updateInventory();
// chunkPropagateSkylightOcclusion.getWhoClicked().setItemOnCursor(chunkPropagateSkylightOcclusion.getCursor());
//
// np.sendError(TranslationUtils.getStaticMsg(ResourceBundle.getBundle("lang.errorMsgs",
// np.getMinecraftLocale().toLocale()), "eventError.cancelledItemDrop"));
// }
// If player tries to drop item by clicking outside of his inventory while dragging the item..
// the PlayerDropItemEvent would cancel this aswell, but this keeps the item being dragged,
// The PlayerDropItemEvent just puts the item back into the inventory, so this is a bit nicer..
// if(ItemStacks.isSoulBound(chunkPropagateSkylightOcclusion.getCursor()) && (chunkPropagateSkylightOcclusion.getSlotType() == InventoryType.SlotType.OUTSIDE))
// {
// chunkPropagateSkylightOcclusion.setCancelled(true);
// ((Player) chunkPropagateSkylightOcclusion.getWhoClicked()).updateInventory();
// chunkPropagateSkylightOcclusion.getWhoClicked().setItemOnCursor(chunkPropagateSkylightOcclusion.getCursor());
//
// np.sendError(TranslationUtils.getStaticMsg(ResourceBundle.getBundle("lang.errorMsgs",
// np.getMinecraftLocale().toLocale()), "eventError.cancelledItemDrop"));
// }
// if(ItemStacks.isSoulBound(chunkPropagateSkylightOcclusion.getCursor()) && chunkPropagateSkylightOcclusion.getSlotType() == InventoryType.SlotType.OUTSIDE &&
// chunkPropagateSkylightOcclusion.getClickedInventory() == null)
// {
//
// }
}
@EventHandler(priority = EventPriority.HIGHEST) // Click the item, and drag it inside the chest
fun onInventoryDrag(e: InventoryDragEvent) {
if(e.whoClicked !is Player) return;
val dragged = e.oldCursor // This is the item that is being dragged
// if (ItemStacks.isSoulBound(dragged))
// {
// int inventorySize = chunkPropagateSkylightOcclusion.getInventory().getSize(); // The size of the inventory, for reference
//
// // Now we go through all of the slots and check if the slot is inside our inventory (using the inventory size as reference)
// for (int i : chunkPropagateSkylightOcclusion.getRawSlots())
// {
// if (i < inventorySize)
// {
// chunkPropagateSkylightOcclusion.setCancelled(true);
//
// np.sendError(TranslationUtils.getStaticMsg(ResourceBundle.getBundle("lang.errorMsgs"
// , np.getMinecraftLocale().toLocale()), "eventError.cancelledPutItemInInventory"));
// break;
// }
// }
// }
val type = e.inventory.type
if (isSoulBound(dragged) &&
type != InventoryType.CREATIVE &&
type != InventoryType.PLAYER &&
type != InventoryType.CRAFTING) {
e.isCancelled = true
(e.whoClicked as Player).sendMessage(WacCore.messages.getString("error.event.cancelled.inventory.putItemIn"));
}
}
// @EventHandler
// public void onInventoryClick(InventoryClickEvent chunkPropagateSkylightOcclusion)
// {
//
// }
@EventHandler // Soulbound items can not be put in any inventory.
fun onInventoryMoveItem(e: InventoryMoveItemEvent) {
val holder = e.initiator.holder
if (holder is Player) {
if (!e.destination.equals(holder.inventory)) {
e.isCancelled = true
holder.updateInventory()
holder.sendMessage(WacCore.messages.getString("error.event.cancelled.inventory.putItemIn"));
}
}
}
@EventHandler // soulbound items can not be used in crafting recipes.
fun onPrepareItemCraft(e: CraftItemEvent) {
if(e.whoClicked !is Player) return;
var containsSoulboundItem = false;
for (i in e.inventory.contents) {
if (i != null && isSoulBound(i)) {
containsSoulboundItem = true;
break; // don't waste time continuing to iterate till the end.
}
}
if (containsSoulboundItem) {
e.isCancelled = true;
(e.whoClicked as Player).updateInventory();
e.whoClicked.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("event.error.cancelled.item.craft.soulbound"));
}
}
// If the player dies, all soulbound drops should be removed from his death drops.
@EventHandler(priority = EventPriority.HIGHEST)
fun onPlayerDeath(e: PlayerDeathEvent) {
// Prevent soulbound items from being dropped on death..
val list = e.drops
val i = list.iterator()
while (i.hasNext()) {
if (isSoulBound(i.next())) i.remove();
}
}
// Soulbound items should not be put on any armor stand.
@EventHandler(priority = EventPriority.HIGHEST)
fun onPlayerArmorStandManipulate(e: PlayerArmorStandManipulateEvent) {
if (e.playerItem != null && isSoulBound(e.playerItem)) {
e.isCancelled = true
e.player.sendMessage(ChatColor.RED.toString() + WacCore.messages.getString("error.event.cancelled.entity.armorStand.putItemOn"));
}
}
@EventHandler
fun onPlayerItemDamage(e: PlayerItemDamageEvent) {
if (isUnbreakable(e.item)) {
e.isCancelled = true;
e.player.updateInventory();
}
}
}
| gpl-3.0 | d0c9ca52f766d1dc02e7f1885f223468 | 43.950192 | 179 | 0.610273 | 4.653861 | false | false | false | false |
scenerygraphics/SciView | src/main/kotlin/sc/iview/controls/behaviours/Ruler.kt | 1 | 4267 | /*-
* #%L
* Scenery-backed 3D visualization package for ImageJ.
* %%
* Copyright (C) 2016 - 2021 SciView developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package sc.iview.controls.behaviours
import graphics.scenery.Mesh
import graphics.scenery.primitives.Line
import graphics.scenery.primitives.TextBoard
import graphics.scenery.utils.LazyLogger
import org.joml.Vector2f
import org.joml.Vector3f
import org.joml.Vector4f
import org.scijava.ui.behaviour.DragBehaviour
import sc.iview.SciView
/**
* Draws a line on a mouse drag - just like when you draw a line in paint.
*
* @author Justin Buerger <[email protected]>
*/
class Ruler(protected val sciView: SciView): DragBehaviour {
//line which is to be drawn
private val line = Line(simple = true)
//position on the mouse click; start of the line
private val origin = Vector3f()
private val finalLength = Vector3f()
private val logger by LazyLogger()
private val cam = sciView.camera
/** Setup the line and delete the old one */
override fun init(p0: Int, p1: Int) {
sciView.deleteNode(line)
origin.set(getMousePositionIn3D(p0, p1))
line.addPoint(origin)
sciView.addNode(line)
sciView.allSceneNodes.forEach {
if(it.name == "DistanceTextBoard" && it is Mesh) {
sciView.removeMesh(it)
}
}
}
/** Drag the line*/
override fun drag(p0: Int, p1: Int) {
val position = getMousePositionIn3D(p0, p1)
line.clearPoints()
line.addPoint(origin)
line.addPoint(position)
}
/**Finish the line*/
override fun end(p0: Int, p1: Int) {
val endPosition = getMousePositionIn3D(p0, p1)
line.clearPoints()
line.addPoint(origin)
line.addPoint(endPosition)
endPosition.sub(origin, finalLength)
logger.info("The line is ${finalLength.length()}")
val board = TextBoard()
board.text = "Distance: ${finalLength.length()}"
board.name = "DistanceTextBoard"
board.transparent = 0
board.fontColor = Vector4f(0.0f, 0.0f, 0.0f, 1.0f)
board.backgroundColor = Vector4f(1f, 1f, 1f, 1.0f)
val boardPosition = Vector3f()
origin.add(endPosition, boardPosition).mul(0.5f)
board.spatial().position = boardPosition.mul(0.5f)
board.spatial().scale = Vector3f(0.3f, 0.3f, 0.3f)
sciView.addNode(board)
}
/** Get the position of your mouse in 3D world coordinates*/
private fun getMousePositionIn3D(p0: Int, p1: Int): Vector3f {
val width = cam!!.width
val height = cam.height
val posX = (p0 - width / 2.0f) / (width / 2.0f)
val posY = -1.0f * (p1 - height / 2.0f) / (height / 2.0f)
val mousePosition = cam.spatial().viewportToView(Vector2f(posX, posY))
val position4D = cam.spatial().viewToWorld(mousePosition)
return Vector3f(position4D.x(), position4D.y(), position4D.z())
}
} | bsd-2-clause | 732fdae784f6502778c25a6ddc27bbeb | 38.518519 | 79 | 0.681978 | 3.799644 | false | false | false | false |
nrizzio/Signal-Android | app/src/main/java/org/thoughtcrime/securesms/avatar/text/TextAvatarCreationFragment.kt | 2 | 5969 | package org.thoughtcrime.securesms.avatar.text
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
import androidx.fragment.app.viewModels
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import androidx.transition.AutoTransition
import androidx.transition.TransitionManager
import com.google.android.material.tabs.TabLayout
import org.signal.core.util.EditTextUtil
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.avatar.Avatar
import org.thoughtcrime.securesms.avatar.AvatarBundler
import org.thoughtcrime.securesms.avatar.AvatarColorItem
import org.thoughtcrime.securesms.avatar.Avatars
import org.thoughtcrime.securesms.avatar.picker.AvatarPickerItem
import org.thoughtcrime.securesms.components.BoldSelectionTabItem
import org.thoughtcrime.securesms.components.ControllableTabLayout
import org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout
import org.thoughtcrime.securesms.components.recyclerview.GridDividerDecoration
import org.thoughtcrime.securesms.util.ViewUtil
import org.thoughtcrime.securesms.util.adapter.mapping.MappingAdapter
/**
* Fragment to create an avatar based off of a Vector or Text (via a pager)
*/
class TextAvatarCreationFragment : Fragment(R.layout.text_avatar_creation_fragment) {
private val viewModel: TextAvatarCreationViewModel by viewModels(factoryProducer = this::createFactory)
private lateinit var textInput: EditText
private lateinit var recycler: RecyclerView
private lateinit var content: ConstraintLayout
private val withRecyclerSet = ConstraintSet()
private val withoutRecyclerSet = ConstraintSet()
private var hasBoundFromViewModel: Boolean = false
private fun createFactory(): TextAvatarCreationViewModel.Factory {
val args = TextAvatarCreationFragmentArgs.fromBundle(requireArguments())
val textBundle = args.textAvatar
val text = if (textBundle != null) {
AvatarBundler.extractText(textBundle)
} else {
Avatar.Text("", Avatars.colors.random(), Avatar.DatabaseId.NotSet)
}
return TextAvatarCreationViewModel.Factory(text)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val toolbar: Toolbar = view.findViewById(R.id.text_avatar_creation_toolbar)
val tabLayout: ControllableTabLayout = view.findViewById(R.id.text_avatar_creation_tabs)
val doneButton: View = view.findViewById(R.id.text_avatar_creation_done)
val keyboardAwareLayout: KeyboardAwareLinearLayout = view.findViewById(R.id.keyboard_aware_layout)
withRecyclerSet.load(requireContext(), R.layout.text_avatar_creation_fragment_content)
withoutRecyclerSet.load(requireContext(), R.layout.text_avatar_creation_fragment_content_hidden_recycler)
content = view.findViewById(R.id.content)
recycler = view.findViewById(R.id.text_avatar_creation_recycler)
textInput = view.findViewById(R.id.avatar_picker_item_text)
toolbar.setNavigationOnClickListener { Navigation.findNavController(it).popBackStack() }
BoldSelectionTabItem.registerListeners(tabLayout)
val onTabSelectedListener = OnTabSelectedListener()
tabLayout.addOnTabSelectedListener(onTabSelectedListener)
onTabSelectedListener.onTabSelected(requireNotNull(tabLayout.getTabAt(tabLayout.selectedTabPosition)))
val adapter = MappingAdapter()
recycler.addItemDecoration(GridDividerDecoration(4, ViewUtil.dpToPx(16)))
AvatarColorItem.registerViewHolder(adapter) {
viewModel.setColor(it)
}
recycler.adapter = adapter
val viewHolder = AvatarPickerItem.ViewHolder(view)
viewModel.state.observe(viewLifecycleOwner) { state ->
EditTextUtil.setCursorColor(textInput, state.currentAvatar.color.foregroundColor)
viewHolder.bind(AvatarPickerItem.Model(state.currentAvatar, false))
adapter.submitList(state.colors().map { AvatarColorItem.Model(it) })
hasBoundFromViewModel = true
}
EditTextUtil.addGraphemeClusterLimitFilter(textInput, 3)
textInput.doAfterTextChanged {
if (it != null && hasBoundFromViewModel) {
viewModel.setText(it.toString())
}
}
doneButton.setOnClickListener { v ->
setFragmentResult(REQUEST_KEY_TEXT, AvatarBundler.bundleText(viewModel.getCurrentAvatar()))
Navigation.findNavController(v).popBackStack()
}
textInput.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_NEXT) {
tabLayout.getTabAt(1)?.select()
true
} else {
false
}
}
keyboardAwareLayout.addOnKeyboardHiddenListener {
if (tabLayout.selectedTabPosition == 1) {
val transition = AutoTransition().setStartDelay(250L)
TransitionManager.endTransitions(content)
withRecyclerSet.applyTo(content)
TransitionManager.beginDelayedTransition(content, transition)
}
}
}
private inner class OnTabSelectedListener : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab) {
when (tab.position) {
0 -> {
textInput.isEnabled = true
ViewUtil.focusAndShowKeyboard(textInput)
withoutRecyclerSet.applyTo(content)
textInput.setSelection(textInput.length())
}
1 -> {
textInput.isEnabled = false
ViewUtil.hideKeyboard(requireContext(), textInput)
}
}
}
override fun onTabUnselected(tab: TabLayout.Tab?) = Unit
override fun onTabReselected(tab: TabLayout.Tab?) = Unit
}
companion object {
const val REQUEST_KEY_TEXT = "org.thoughtcrime.securesms.avatar.text.TEXT"
}
}
| gpl-3.0 | fd1027cb22e537f4dbdb86d1ba8df914 | 38.269737 | 109 | 0.769643 | 4.748608 | false | false | false | false |
androidx/androidx | paging/paging-runtime/src/main/java/androidx/paging/PagingDataAdapter.kt | 3 | 19399 | /*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.paging
import androidx.annotation.IntRange
import androidx.annotation.MainThread
import androidx.lifecycle.Lifecycle
import androidx.paging.LoadState.NotLoading
import androidx.paging.LoadType.REFRESH
import androidx.recyclerview.widget.AdapterListUpdateCallback
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy.ALLOW
import androidx.recyclerview.widget.RecyclerView.Adapter.StateRestorationPolicy.PREVENT
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
/**
* [RecyclerView.Adapter] base class for presenting paged data from [PagingData]s in
* a [RecyclerView].
*
* This class is a convenience wrapper around [AsyncPagingDataDiffer] that implements common default
* behavior for item counting, and listening to update events.
*
* To present a [Pager], use [collectLatest][kotlinx.coroutines.flow.collectLatest] to observe
* [Pager.flow] and call [submitData] whenever a new [PagingData] is emitted.
*
* If using RxJava and LiveData extensions on [Pager], use the non-suspending overload of
* [submitData], which accepts a [Lifecycle].
*
* [PagingDataAdapter] listens to internal [PagingData] loading events as
* [pages][PagingSource.LoadResult.Page] are loaded, and uses [DiffUtil] on a background thread to
* compute fine grained updates as updated content in the form of new PagingData objects are
* received.
*
* *State Restoration*: To be able to restore [RecyclerView] state (e.g. scroll position) after a
* configuration change / application recreate, [PagingDataAdapter] calls
* [RecyclerView.Adapter.setStateRestorationPolicy] with
* [RecyclerView.Adapter.StateRestorationPolicy.PREVENT] upon initialization and waits for the
* first page to load before allowing state restoration.
* Any other call to [RecyclerView.Adapter.setStateRestorationPolicy] by the application will
* disable this logic and will rely on the user set value.
*
* @sample androidx.paging.samples.pagingDataAdapterSample
*/
abstract class PagingDataAdapter<T : Any, VH : RecyclerView.ViewHolder>
/**
* Construct a [PagingDataAdapter].
*
* @param mainDispatcher [CoroutineContext] where UI events are dispatched. Typically, this should be
* [Dispatchers.Main].
* @param workerDispatcher [CoroutineContext] where the work to generate UI events is dispatched, for
* example when diffing lists on [REFRESH]. Typically, this should have a background
* [CoroutineDispatcher] set; [Dispatchers.Default] by default.
* @param diffCallback Callback for calculating the diff between two non-disjoint lists on
* [REFRESH]. Used as a fallback for item-level diffing when Paging is unable to find a faster path
* for generating the UI events required to display the new list.
*/
@JvmOverloads
constructor(
diffCallback: DiffUtil.ItemCallback<T>,
mainDispatcher: CoroutineContext = Dispatchers.Main,
workerDispatcher: CoroutineContext = Dispatchers.Default,
) : RecyclerView.Adapter<VH>() {
/**
* Construct a [PagingDataAdapter].
*
* @param diffCallback Callback for calculating the diff between two non-disjoint lists on
* [REFRESH]. Used as a fallback for item-level diffing when Paging is unable to find a faster
* path for generating the UI events required to display the new list.
* @param mainDispatcher [CoroutineDispatcher] where UI events are dispatched. Typically,
* this should be [Dispatchers.Main].
*/
@Deprecated(
message = "Superseded by constructors which accept CoroutineContext",
level = DeprecationLevel.HIDDEN
)
// Only for binary compatibility; cannot apply @JvmOverloads as the function signature would
// conflict with the primary constructor.
@Suppress("MissingJvmstatic")
constructor(
diffCallback: DiffUtil.ItemCallback<T>,
mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
) : this(
diffCallback = diffCallback,
mainDispatcher = mainDispatcher,
workerDispatcher = Dispatchers.Default,
)
/**
* Construct a [PagingDataAdapter].
*
* @param diffCallback Callback for calculating the diff between two non-disjoint lists on
* [REFRESH]. Used as a fallback for item-level diffing when Paging is unable to find a faster
* path for generating the UI events required to display the new list.
* @param mainDispatcher [CoroutineDispatcher] where UI events are dispatched. Typically,
* this should be [Dispatchers.Main].
* @param workerDispatcher [CoroutineDispatcher] where the work to generate UI events is
* dispatched, for example when diffing lists on [REFRESH]. Typically, this should dispatch on a
* background thread; [Dispatchers.Default] by default.
*/
@Deprecated(
message = "Superseded by constructors which accept CoroutineContext",
level = DeprecationLevel.HIDDEN
)
// Only for binary compatibility; cannot apply @JvmOverloads as the function signature would
// conflict with the primary constructor.
@Suppress("MissingJvmstatic")
constructor(
diffCallback: DiffUtil.ItemCallback<T>,
mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
workerDispatcher: CoroutineDispatcher = Dispatchers.Default,
) : this(
diffCallback = diffCallback,
mainDispatcher = mainDispatcher,
workerDispatcher = workerDispatcher,
)
/**
* Track whether developer called [setStateRestorationPolicy] or not to decide whether the
* automated state restoration should apply or not.
*/
private var userSetRestorationPolicy = false
override fun setStateRestorationPolicy(strategy: StateRestorationPolicy) {
userSetRestorationPolicy = true
super.setStateRestorationPolicy(strategy)
}
private val differ = AsyncPagingDataDiffer(
diffCallback = diffCallback,
updateCallback = AdapterListUpdateCallback(this),
mainDispatcher = mainDispatcher,
workerDispatcher = workerDispatcher
)
init {
// Wait on state restoration until the first insert event.
super.setStateRestorationPolicy(PREVENT)
fun considerAllowingStateRestoration() {
if (stateRestorationPolicy == PREVENT && !userSetRestorationPolicy) {
[email protected] = ALLOW
}
}
// Watch for adapter insert before triggering state restoration. This is almost redundant
// with loadState below, but can handle cached case.
@Suppress("LeakingThis")
registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
considerAllowingStateRestoration()
unregisterAdapterDataObserver(this)
super.onItemRangeInserted(positionStart, itemCount)
}
})
// Watch for loadState update before triggering state restoration. This is almost
// redundant with data observer above, but can handle empty page case.
addLoadStateListener(object : Function1<CombinedLoadStates, Unit> {
// Ignore the first event we get, which is always the initial state, since we only
// want to observe for Insert events.
private var ignoreNextEvent = true
override fun invoke(loadStates: CombinedLoadStates) {
if (ignoreNextEvent) {
ignoreNextEvent = false
} else if (loadStates.source.refresh is NotLoading) {
considerAllowingStateRestoration()
removeLoadStateListener(this)
}
}
})
}
/**
* Note: [getItemId] is final, because stable IDs are unnecessary and therefore unsupported.
*
* [PagingDataAdapter]'s async diffing means that efficient change animations are handled for
* you, without the performance drawbacks of [RecyclerView.Adapter.notifyDataSetChanged].
* Instead, the diffCallback parameter of the [PagingDataAdapter] serves the same
* functionality - informing the adapter and [RecyclerView] how items are changed and moved.
*/
final override fun getItemId(position: Int): Long {
return super.getItemId(position)
}
/**
* Stable ids are unsupported by [PagingDataAdapter]. Calling this method is an error and will
* result in an [UnsupportedOperationException].
*
* @param hasStableIds Whether items in data set have unique identifiers or not.
*
* @throws UnsupportedOperationException Always thrown, since this is unsupported by
* [PagingDataAdapter].
*/
final override fun setHasStableIds(hasStableIds: Boolean) {
throw UnsupportedOperationException("Stable ids are unsupported on PagingDataAdapter.")
}
/**
* Present a [PagingData] until it is invalidated by a call to [refresh] or
* [PagingSource.invalidate].
*
* This method is typically used when collecting from a [Flow] produced by [Pager]. For RxJava
* or LiveData support, use the non-suspending overload of [submitData], which accepts a
* [Lifecycle].
*
* Note: This method suspends while it is actively presenting page loads from a [PagingData],
* until the [PagingData] is invalidated. Although cancellation will propagate to this call
* automatically, collecting from a [Pager.flow] with the intention of presenting the most
* up-to-date representation of your backing dataset should typically be done using
* [collectLatest][kotlinx.coroutines.flow.collectLatest].
*
* @sample androidx.paging.samples.submitDataFlowSample
*
* @see [Pager]
*/
suspend fun submitData(pagingData: PagingData<T>) {
differ.submitData(pagingData)
}
/**
* Present a [PagingData] until it is either invalidated or another call to [submitData] is
* made.
*
* This method is typically used when observing a RxJava or LiveData stream produced by [Pager].
* For [Flow] support, use the suspending overload of [submitData], which automates cancellation
* via [CoroutineScope][kotlinx.coroutines.CoroutineScope] instead of relying of [Lifecycle].
*
* @sample androidx.paging.samples.submitDataLiveDataSample
* @sample androidx.paging.samples.submitDataRxSample
*
* @see submitData
* @see [Pager]
*/
fun submitData(lifecycle: Lifecycle, pagingData: PagingData<T>) {
differ.submitData(lifecycle, pagingData)
}
/**
* Retry any failed load requests that would result in a [LoadState.Error] update to this
* [PagingDataAdapter].
*
* Unlike [refresh], this does not invalidate [PagingSource], it only retries failed loads
* within the same generation of [PagingData].
*
* [LoadState.Error] can be generated from two types of load requests:
* * [PagingSource.load] returning [PagingSource.LoadResult.Error]
* * [RemoteMediator.load] returning [RemoteMediator.MediatorResult.Error]
*/
fun retry() {
differ.retry()
}
/**
* Refresh the data presented by this [PagingDataAdapter].
*
* [refresh] triggers the creation of a new [PagingData] with a new instance of [PagingSource]
* to represent an updated snapshot of the backing dataset. If a [RemoteMediator] is set,
* calling [refresh] will also trigger a call to [RemoteMediator.load] with [LoadType] [REFRESH]
* to allow [RemoteMediator] to check for updates to the dataset backing [PagingSource].
*
* Note: This API is intended for UI-driven refresh signals, such as swipe-to-refresh.
* Invalidation due repository-layer signals, such as DB-updates, should instead use
* [PagingSource.invalidate].
*
* @see PagingSource.invalidate
*
* @sample androidx.paging.samples.refreshSample
*/
fun refresh() {
differ.refresh()
}
/**
* Returns the presented item at the specified position, notifying Paging of the item access to
* trigger any loads necessary to fulfill [prefetchDistance][PagingConfig.prefetchDistance].
*
* @param position Index of the presented item to return, including placeholders.
* @return The presented item at [position], `null` if it is a placeholder
*/
@MainThread
protected fun getItem(@IntRange(from = 0) position: Int) = differ.getItem(position)
/**
* Returns the presented item at the specified position, without notifying Paging of the item
* access that would normally trigger page loads.
*
* @param index Index of the presented item to return, including placeholders.
* @return The presented item at position [index], `null` if it is a placeholder.
*/
@MainThread
fun peek(@IntRange(from = 0) index: Int) = differ.peek(index)
/**
* Returns a new [ItemSnapshotList] representing the currently presented items, including any
* placeholders if they are enabled.
*/
fun snapshot(): ItemSnapshotList<T> = differ.snapshot()
override fun getItemCount() = differ.itemCount
/**
* A hot [Flow] of [CombinedLoadStates] that emits a snapshot whenever the loading state of the
* current [PagingData] changes.
*
* This flow is conflated, so it buffers the last update to [CombinedLoadStates] and
* immediately delivers the current load states on collection.
*/
val loadStateFlow: Flow<CombinedLoadStates> = differ.loadStateFlow
/**
* A hot [Flow] that emits after the pages presented to the UI are updated, even if the
* actual items presented don't change.
*
* An update is triggered from one of the following:
* * [submitData] is called and initial load completes, regardless of any differences in
* the loaded data
* * A [Page][androidx.paging.PagingSource.LoadResult.Page] is inserted
* * A [Page][androidx.paging.PagingSource.LoadResult.Page] is dropped
*
* Note: This is a [SharedFlow][kotlinx.coroutines.flow.SharedFlow] configured to replay
* 0 items with a buffer of size 64. If a collector lags behind page updates, it may
* trigger multiple times for each intermediate update that was presented while your collector
* was still working. To avoid this behavior, you can
* [conflate][kotlinx.coroutines.flow.conflate] this [Flow] so that you only receive the latest
* update, which is useful in cases where you are simply updating UI and don't care about
* tracking the exact number of page updates.
*/
val onPagesUpdatedFlow: Flow<Unit> = differ.onPagesUpdatedFlow
/**
* Add a [CombinedLoadStates] listener to observe the loading state of the current [PagingData].
*
* As new [PagingData] generations are submitted and displayed, the listener will be notified to
* reflect the current [CombinedLoadStates].
*
* @param listener [LoadStates] listener to receive updates.
*
* @see removeLoadStateListener
* @sample androidx.paging.samples.addLoadStateListenerSample
*/
fun addLoadStateListener(listener: (CombinedLoadStates) -> Unit) {
differ.addLoadStateListener(listener)
}
/**
* Remove a previously registered [CombinedLoadStates] listener.
*
* @param listener Previously registered listener.
* @see addLoadStateListener
*/
fun removeLoadStateListener(listener: (CombinedLoadStates) -> Unit) {
differ.removeLoadStateListener(listener)
}
/**
* Add a listener which triggers after the pages presented to the UI are updated, even if the
* actual items presented don't change.
*
* An update is triggered from one of the following:
* * [submitData] is called and initial load completes, regardless of any differences in
* the loaded data
* * A [Page][androidx.paging.PagingSource.LoadResult.Page] is inserted
* * A [Page][androidx.paging.PagingSource.LoadResult.Page] is dropped
*
* @param listener called after pages presented are updated.
*
* @see removeOnPagesUpdatedListener
*/
fun addOnPagesUpdatedListener(listener: () -> Unit) {
differ.addOnPagesUpdatedListener(listener)
}
/**
* Remove a previously registered listener for new [PagingData] generations completing
* initial load and presenting to the UI.
*
* @param listener Previously registered listener.
*
* @see addOnPagesUpdatedListener
*/
fun removeOnPagesUpdatedListener(listener: () -> Unit) {
differ.removeOnPagesUpdatedListener(listener)
}
/**
* Create a [ConcatAdapter] with the provided [LoadStateAdapter]s displaying the
* [LoadType.PREPEND] [LoadState] as a list item at the end of the presented list.
*
* @see LoadStateAdapter
* @see withLoadStateHeaderAndFooter
* @see withLoadStateFooter
*/
fun withLoadStateHeader(
header: LoadStateAdapter<*>
): ConcatAdapter {
addLoadStateListener { loadStates ->
header.loadState = loadStates.prepend
}
return ConcatAdapter(header, this)
}
/**
* Create a [ConcatAdapter] with the provided [LoadStateAdapter]s displaying the
* [LoadType.APPEND] [LoadState] as a list item at the start of the presented list.
*
* @see LoadStateAdapter
* @see withLoadStateHeaderAndFooter
* @see withLoadStateHeader
*/
fun withLoadStateFooter(
footer: LoadStateAdapter<*>
): ConcatAdapter {
addLoadStateListener { loadStates ->
footer.loadState = loadStates.append
}
return ConcatAdapter(this, footer)
}
/**
* Create a [ConcatAdapter] with the provided [LoadStateAdapter]s displaying the
* [LoadType.PREPEND] and [LoadType.APPEND] [LoadState]s as list items at the start and end
* respectively.
*
* @see LoadStateAdapter
* @see withLoadStateHeader
* @see withLoadStateFooter
*/
fun withLoadStateHeaderAndFooter(
header: LoadStateAdapter<*>,
footer: LoadStateAdapter<*>
): ConcatAdapter {
addLoadStateListener { loadStates ->
header.loadState = loadStates.prepend
footer.loadState = loadStates.append
}
return ConcatAdapter(header, this, footer)
}
}
| apache-2.0 | 0ac366f16bbdf2d6e8b7264f071031ab | 41.263617 | 101 | 0.701737 | 4.885167 | false | false | false | false |
DSteve595/Put.io | app/src/main/java/com/stevenschoen/putionew/tv/HorizontalStackLayout.kt | 1 | 1794 | package com.stevenschoen.putionew.tv
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.stevenschoen.putionew.R
import com.stevenschoen.putionew.UIUtils
class HorizontalStackLayout : ViewGroup {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
val layerOffset by lazy { resources.getDimensionPixelSize(R.dimen.tv_layer_offset) }
val layerElevation by lazy { resources.getDimension(R.dimen.tv_layer_elevation) }
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
val childLeft = paddingLeft;
val childTop = paddingTop;
val childRight = measuredWidth - paddingRight;
val childBottom = measuredHeight - paddingBottom;
val childWidth = childRight - childLeft;
val childHeight = childBottom - childTop;
for (i in 0..childCount - 1) {
val child = getChildAt(i)
val offset = (i * layerOffset)
child.measure(MeasureSpec.makeMeasureSpec(childWidth - offset, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));
child.layout(childLeft + offset, childTop, childRight, childBottom)
if (UIUtils.hasLollipop()) {
child.elevation = (i * layerElevation)
}
}
}
// Hacky, but without it sometimes the d-pad can go to the wrong page and become stuck
override fun focusSearch(focused: View?, direction: Int): View? {
return null
}
}
| mit | 2d853f8910c9a50b49e9d9a70515b0ec | 40.72093 | 154 | 0.740803 | 4.251185 | false | false | false | false |
GunoH/intellij-community | plugins/kotlin/idea/src/org/jetbrains/kotlin/idea/refactoring/changeSignature/KotlinChangeSignatureProcessor.kt | 4 | 8016 | // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.refactoring.changeSignature
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.NlsContexts
import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.refactoring.RefactoringBundle
import com.intellij.refactoring.changeSignature.ChangeSignatureProcessorBase
import com.intellij.refactoring.changeSignature.ChangeSignatureUsageProcessor
import com.intellij.refactoring.changeSignature.JavaChangeSignatureUsageProcessor
import com.intellij.refactoring.rename.RenameUtil
import com.intellij.refactoring.rename.UnresolvableCollisionUsageInfo
import com.intellij.usageView.UsageInfo
import com.intellij.usageView.UsageViewDescriptor
import com.intellij.util.containers.MultiMap
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.idea.base.resources.KotlinBundle
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeInsight.shorten.performDelayedRefactoringRequests
import org.jetbrains.kotlin.idea.core.canMoveLambdaOutsideParentheses
import org.jetbrains.kotlin.idea.core.isOverridable
import org.jetbrains.kotlin.idea.core.moveFunctionLiteralOutsideParentheses
import org.jetbrains.kotlin.idea.refactoring.broadcastRefactoringExit
import org.jetbrains.kotlin.idea.refactoring.changeSignature.usages.*
import org.jetbrains.kotlin.psi.KtCallExpression
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
class KotlinChangeSignatureProcessor(
project: Project,
changeInfo: KotlinChangeInfo,
@NlsContexts.Command private val commandName: String
) : ChangeSignatureProcessorBase(project, KotlinChangeInfoWrapper(changeInfo)) {
init {
// we must force collecting references to other parameters now before the signature is changed
changeInfo.newParameters.forEach { it.defaultValueParameterReferences }
}
val ktChangeInfo: KotlinChangeInfo
get() = changeInfo.delegate!!
override fun setPrepareSuccessfulSwingThreadCallback(callback: Runnable?) {
val actualCallback = if (callback != null) {
Runnable {
callback.run()
setPrepareSuccessfulSwingThreadCallback(null)
}
} else null
super.setPrepareSuccessfulSwingThreadCallback(actualCallback)
}
override fun createUsageViewDescriptor(usages: Array<UsageInfo>): UsageViewDescriptor {
val subject = if (ktChangeInfo.kind.isConstructor)
KotlinBundle.message("text.constructor")
else
KotlinBundle.message("text.function")
return KotlinUsagesViewDescriptor(myChangeInfo.method, RefactoringBundle.message("0.to.change.signature", subject))
}
override fun getChangeInfo(): KotlinChangeInfoWrapper = super.getChangeInfo() as KotlinChangeInfoWrapper
override fun findUsages(): Array<UsageInfo> {
val allUsages = ArrayList<UsageInfo>()
val javaUsages = mutableSetOf<UsageInfo>()
ktChangeInfo.getOrCreateJavaChangeInfos()?.let { javaChangeInfos ->
val javaProcessor = JavaChangeSignatureUsageProcessor()
javaChangeInfos.mapNotNullTo(allUsages) { javaChangeInfo ->
val javaUsagesForKtChange = javaProcessor.findUsages(javaChangeInfo)
val uniqueJavaUsagesForKtChange = javaUsagesForKtChange.filter<UsageInfo> {
it.element?.language != KotlinLanguage.INSTANCE && !javaUsages.contains(it)
}.ifEmpty { return@mapNotNullTo null }
javaUsages.addAll(uniqueJavaUsagesForKtChange)
KotlinWrapperForJavaUsageInfos(ktChangeInfo, javaChangeInfo, uniqueJavaUsagesForKtChange.toTypedArray(), changeInfo.method)
}
}
val primaryConstructor = ktChangeInfo.method as? KtPrimaryConstructor
if (primaryConstructor != null) {
findConstructorPropertyUsages(primaryConstructor, allUsages)
}
super.findUsages().filterTo(allUsages) { it is KotlinUsageInfo<*> || it is UnresolvableCollisionUsageInfo }
return allUsages.toTypedArray()
}
private fun findConstructorPropertyUsages(
primaryConstructor: KtPrimaryConstructor,
allUsages: ArrayList<UsageInfo>
) {
for ((index, parameter) in primaryConstructor.valueParameters.withIndex()) {
if (!parameter.isOverridable) continue
val parameterInfo = ktChangeInfo.newParameters.find { it.originalIndex == index } ?: continue
val descriptor = parameter.resolveToDescriptorIfAny() as? PropertyDescriptor ?: continue
val methodDescriptor = KotlinChangeSignatureData(
descriptor,
parameter,
listOf(descriptor),
)
val propertyChangeInfo = KotlinChangeInfo(
methodDescriptor,
name = parameterInfo.name,
newReturnTypeInfo = parameterInfo.currentTypeInfo,
context = parameter,
)
ktChangeInfo.registerInnerChangeInfo(propertyChangeInfo)
KotlinChangeSignatureProcessor(myProject, propertyChangeInfo, commandName).findUsages().mapNotNullTo(allUsages) {
if (it is KotlinWrapperForJavaUsageInfos) return@mapNotNullTo it
val element = it.element
if (element != null && !(it is KotlinCallableDefinitionUsage<*> && element == parameter))
KotlinWrapperForPropertyInheritorsUsage(propertyChangeInfo, it, element)
else
null
}
}
}
override fun preprocessUsages(refUsages: Ref<Array<UsageInfo>>): Boolean {
val usageProcessors = ChangeSignatureUsageProcessor.EP_NAME.extensions
if (!usageProcessors.all { it.setupDefaultValues(myChangeInfo, refUsages, myProject) }) return false
val conflictDescriptions = object : MultiMap<PsiElement, String>() {
override fun createCollection() = LinkedHashSet<String>()
}
usageProcessors.forEach { conflictDescriptions.putAllValues(it.findConflicts(myChangeInfo, refUsages)) }
val usages = refUsages.get()
val usagesSet = usages.toHashSet()
RenameUtil.addConflictDescriptions(usages, conflictDescriptions)
RenameUtil.removeConflictUsages(usagesSet)
val usageArray = usagesSet.sortedWith(Comparator { u1, u2 ->
if (u1 is KotlinImplicitReceiverUsage && u2 is KotlinFunctionCallUsage) return@Comparator -1
if (u2 is KotlinImplicitReceiverUsage && u1 is KotlinFunctionCallUsage) return@Comparator 1
val element1 = u1.element
val element2 = u2.element
val rank1 = element1?.textOffset ?: -1
val rank2 = element2?.textOffset ?: -1
rank2 - rank1 // Reverse order
}).toTypedArray()
refUsages.set(usageArray)
return showConflicts(conflictDescriptions, usageArray)
}
override fun isPreviewUsages(usages: Array<out UsageInfo>): Boolean = isPreviewUsages
override fun getCommandName() = commandName
override fun performRefactoring(usages: Array<out UsageInfo>) = try {
super.performRefactoring(usages)
usages.forEach {
val callExpression = it.element as? KtCallExpression ?: return@forEach
if (callExpression.canMoveLambdaOutsideParentheses()) {
callExpression.moveFunctionLiteralOutsideParentheses()
}
}
performDelayedRefactoringRequests(myProject)
} finally {
changeInfo.invalidate()
}
override fun doRun() = try {
super.doRun()
} finally {
broadcastRefactoringExit(myProject, refactoringId!!)
}
}
| apache-2.0 | 7a5e058e0a6a15ff24a058ff29698ea8 | 44.033708 | 158 | 0.713323 | 5.497942 | false | false | false | false |
GunoH/intellij-community | java/java-impl/src/com/intellij/lang/java/actions/CreateConstructorAction.kt | 1 | 5870 | // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.lang.java.actions
import com.intellij.codeInsight.CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement
import com.intellij.codeInsight.daemon.QuickFixBundle.message
import com.intellij.codeInsight.daemon.impl.quickfix.CreateClassFromNewFix.setupSuperCall
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageBaseFix
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageUtils
import com.intellij.codeInsight.daemon.impl.quickfix.GuessTypeParameters
import com.intellij.codeInsight.generation.OverrideImplementUtil
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
import com.intellij.codeInsight.intention.preview.IntentionPreviewUtils
import com.intellij.codeInsight.template.Template
import com.intellij.codeInsight.template.TemplateBuilder
import com.intellij.codeInsight.template.TemplateBuilderImpl
import com.intellij.codeInsight.template.TemplateEditingAdapter
import com.intellij.lang.java.request.CreateConstructorFromJavaUsageRequest
import com.intellij.lang.jvm.actions.CreateConstructorRequest
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import com.intellij.psi.presentation.java.ClassPresentationUtil.getNameForClass
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtil
internal class CreateConstructorAction(
target: PsiClass,
override val request: CreateConstructorRequest
) : CreateMemberAction(target, request) {
override fun getFamilyName(): String = message("create.constructor.family")
override fun getText(): String = if (request is CreateConstructorFromJavaUsageRequest) {
message("create.constructor.from.new.text")
}
else {
message("create.constructor.text", getNameForClass(target, false))
}
private fun constructorRenderer(project: Project) = JavaConstructorRenderer(project, target, request)
override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo {
val copyClass = PsiTreeUtil.findSameElementInCopy(target, file)
val javaFieldRenderer = JavaConstructorRenderer(project, copyClass, request)
javaFieldRenderer.doMagic()
return IntentionPreviewInfo.DIFF
}
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
constructorRenderer(project).doMagic()
}
}
private class JavaConstructorRenderer(
private val project: Project,
private val targetClass: PsiClass,
private val request: CreateConstructorRequest
) {
private val factory = JavaPsiFacade.getElementFactory(project)!!
fun doMagic() {
//calculate expected parameter types before constructor is inserted
//to avoid possible overload conflicts
val parameters = request.expectedParameters
var constructor = renderConstructor()
constructor = insertConstructor(constructor)
constructor = forcePsiPostprocessAndRestoreElement(constructor) ?: return
val builder = TemplateBuilderImpl(constructor)
createTemplateContext(builder).setupParameters(constructor, parameters)
val superConstructor = setupSuperCall(targetClass, constructor, builder)
constructor = forcePsiPostprocessAndRestoreElement(constructor) ?: return
val template = builder.buildInlineTemplate()
startTemplate(constructor, template, superConstructor)
}
private fun createTemplateContext(builder: TemplateBuilder): TemplateContext {
val guesserContext = (request as? CreateConstructorFromJavaUsageRequest)?.context
val substitutor = request.targetSubstitutor.toPsiSubstitutor(project)
val guesser = GuessTypeParameters(project, factory, builder, substitutor)
return TemplateContext(project, factory, targetClass, builder, guesser, guesserContext)
}
fun renderConstructor(): PsiMethod {
val constructor = factory.createConstructor()
for (modifier in request.modifiers) {
PsiUtil.setModifierProperty(constructor, modifier.toPsiModifier(), true)
}
for (annotation in request.annotations) {
constructor.modifierList.addAnnotation(annotation.qualifiedName)
}
return constructor
}
private fun insertConstructor(constructor: PsiMethod): PsiMethod {
return targetClass.add(constructor) as PsiMethod
}
private fun startTemplate(constructor: PsiMethod, template: Template, superConstructor: PsiMethod?) {
val targetFile = targetClass.containingFile
val targetEditor = CreateFromUsageBaseFix.positionCursor(project, targetFile, constructor) ?: return
val templateListener = object : TemplateEditingAdapter() {
override fun templateFinished(template: Template, brokenOff: Boolean) {
if (brokenOff) return
if (IntentionPreviewUtils.isIntentionPreviewActive()) {
setupBody()
} else {
WriteCommandAction.runWriteCommandAction(project, message("create.constructor.body.command"), null, { setupBody() }, targetFile)
}
}
private fun setupBody() {
PsiDocumentManager.getInstance(project).commitDocument(targetEditor.document)
val offset = targetEditor.caretModel.offset
val newConstructor = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod::class.java, false) ?: return
if (superConstructor == null) {
CreateFromUsageUtils.setupMethodBody(newConstructor)
}
else {
OverrideImplementUtil.setupMethodBody(newConstructor, superConstructor, targetClass)
}
CreateFromUsageUtils.setupEditor(newConstructor, targetEditor)
}
}
CreateFromUsageBaseFix.startTemplate(targetEditor, template, project, templateListener, null)
}
}
| apache-2.0 | 84a7daaa08ba3b17ff76faaf6a800224 | 42.80597 | 140 | 0.786712 | 5.14461 | false | false | false | false |
GunoH/intellij-community | plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/findUsages/OptionsParser.kt | 3 | 8269 | // Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.findUsages
import com.intellij.find.findUsages.*
import com.intellij.openapi.project.Project
import com.intellij.psi.*
import org.jetbrains.kotlin.idea.findUsages.KotlinClassFindUsagesOptions
import org.jetbrains.kotlin.idea.findUsages.KotlinFunctionFindUsagesOptions
import org.jetbrains.kotlin.idea.findUsages.KotlinPropertyFindUsagesOptions
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.idea.test.InTextDirectivesUtils
internal enum class OptionsParser {
CLASS {
override fun parse(text: String, project: Project): FindUsagesOptions {
return KotlinClassFindUsagesOptions(project).apply {
isUsages = false
isSearchForTextOccurrences = false
searchConstructorUsages = false
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
when (s) {
"constructorUsages" -> searchConstructorUsages = true
"derivedInterfaces" -> isDerivedInterfaces = true
"derivedClasses" -> isDerivedClasses = true
"functionUsages" -> isMethodsUsages = true
"propertyUsages" -> isFieldsUsages = true
"expected" -> searchExpected = true
else -> throw IllegalStateException("Invalid option: $s")
}
}
}
}
},
FUNCTION {
override fun parse(text: String, project: Project): FindUsagesOptions {
return KotlinFunctionFindUsagesOptions(project).apply {
isUsages = false
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
when (s) {
"overrides" -> {
isOverridingMethods = true
isImplementingMethods = true
}
"overloadUsages" -> {
isIncludeOverloadUsages = true
isUsages = true
}
"expected" -> {
searchExpected = true
isUsages = true
}
else -> throw IllegalStateException("Invalid option: $s")
}
}
}
}
},
PROPERTY {
override fun parse(text: String, project: Project): FindUsagesOptions {
return KotlinPropertyFindUsagesOptions(project).apply {
isUsages = false
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
when (s) {
"overrides" -> searchOverrides = true
"skipRead" -> isReadAccess = false
"skipWrite" -> isWriteAccess = false
"expected" -> searchExpected = true
else -> throw IllegalStateException("Invalid option: $s")
}
}
}
}
},
JAVA_CLASS {
override fun parse(text: String, project: Project): FindUsagesOptions {
return KotlinClassFindUsagesOptions(project).apply {
isUsages = false
searchConstructorUsages = false
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
when (s) {
"derivedInterfaces" -> isDerivedInterfaces = true
"derivedClasses" -> isDerivedClasses = true
"implementingClasses" -> isImplementingClasses = true
"methodUsages" -> isMethodsUsages = true
"fieldUsages" -> isFieldsUsages = true
else -> throw IllegalStateException("Invalid option: $s")
}
}
}
}
},
JAVA_METHOD {
override fun parse(text: String, project: Project): FindUsagesOptions {
return JavaMethodFindUsagesOptions(project).apply {
isUsages = false
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
when (s) {
"overrides" -> {
isOverridingMethods = true
isImplementingMethods = true
}
else -> throw IllegalStateException("Invalid option: $s")
}
}
}
}
},
JAVA_FIELD {
override fun parse(text: String, project: Project): FindUsagesOptions {
return JavaVariableFindUsagesOptions(project).apply {
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
when (s) {
"skipRead" -> isReadAccess = false
"skipWrite" -> isWriteAccess = false
else -> throw IllegalStateException("Invalid option: `$s`")
}
}
}
}
},
JAVA_PACKAGE {
override fun parse(text: String, project: Project): FindUsagesOptions {
return JavaPackageFindUsagesOptions(project).apply {
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
throw IllegalStateException("Invalid option: `$s`")
}
}
}
},
DEFAULT {
override fun parse(text: String, project: Project): FindUsagesOptions {
return FindUsagesOptions(project).apply {
for (s in InTextDirectivesUtils.findListWithPrefixes(text, "// OPTIONS: ")) {
if (parseCommonOptions(this, s)) continue
throw IllegalStateException("Invalid option: `$s`")
}
}
}
};
abstract fun parse(text: String, project: Project): FindUsagesOptions
companion object {
protected fun parseCommonOptions(options: JavaFindUsagesOptions, s: String): Boolean {
if (parseCommonOptions(options as FindUsagesOptions, s)) {
return true
}
return when (s) {
"skipImports" -> {
options.isSkipImportStatements = true
true
}
else -> false
}
}
protected fun parseCommonOptions(options: FindUsagesOptions, s: String): Boolean {
return when (s) {
"usages" -> {
options.isUsages = true
true
}
"textOccurrences" -> {
options.isSearchForTextOccurrences = true
true
}
else -> false
}
}
fun getParserByPsiElementClass(klass: Class<out PsiElement>): OptionsParser? {
return when (klass) {
KtNamedFunction::class.java -> FUNCTION
KtProperty::class.java, KtParameter::class.java -> PROPERTY
KtClass::class.java -> CLASS
PsiMethod::class.java -> JAVA_METHOD
PsiClass::class.java -> JAVA_CLASS
PsiField::class.java -> JAVA_FIELD
PsiPackage::class.java -> JAVA_PACKAGE
KtTypeParameter::class.java -> DEFAULT
else -> null
}
}
}
} | apache-2.0 | 728f2c431121a5d8be64748270ed0e75 | 39.738916 | 158 | 0.510582 | 6.250189 | false | false | false | false |
WillowChat/Kale | src/main/kotlin/chat/willow/kale/core/tag/extension/AccountTag.kt | 2 | 627 | package chat.willow.kale.core.tag.extension
import chat.willow.kale.core.tag.ITagParser
import chat.willow.kale.core.tag.ITagSerialiser
import chat.willow.kale.core.tag.Tag
data class AccountTag(val account: String) {
companion object Factory: ITagParser<AccountTag>, ITagSerialiser<AccountTag> {
val name = "account"
override fun parse(tag: Tag): AccountTag? {
val value = tag.value ?: return null
return AccountTag(account = value)
}
override fun serialise(tag: AccountTag): Tag? {
return Tag(name = name, value = tag.account)
}
}
} | isc | cd316b3472d0782d835ad9240149e633 | 24.12 | 82 | 0.660287 | 3.91875 | false | false | false | false |
TachiWeb/TachiWeb-Server | TachiServer/src/main/java/xyz/nulldev/ts/syncdeploy/AccountPage.kt | 1 | 11757 | package xyz.nulldev.ts.syncdeploy
import com.github.salomonbrys.kodein.Kodein
import com.github.salomonbrys.kodein.conf.global
import com.github.salomonbrys.kodein.instance
import kotlinx.html.*
import spark.Request
import spark.Response
import spark.Route
import xyz.nulldev.ts.config.ConfigManager
class AccountPage(val am: AccountManager,
val mainPagePath: String): Route {
private val syncConfig = Kodein.global.instance<ConfigManager>().module<SyncConfigModule>()
override fun handle(request: Request, response: Response): Any {
val username = request.cookie("username")
val token = request.cookie("token")
// Redirect users who are not logged in
if(username == null || token == null || !am.authToken(username, token)) {
response.redirect(mainPagePath)
return ""
}
return SiteTemplate(mainPagePath).build(syncConfig.name + " - Dashboard") {
div("ui center aligned container") {
h1("ui dividing header") { +"Dashboard" }
h2("ui header") {
+"Credentials"
}
table("ui definition table collapsing") {
style = "margin: auto"
tbody {
tr {
td { +"Server URL" }
td {
div("ui action input server-url") {
style = "width:350px"
input(InputType.text) {
value = "${syncConfig.baseUrl}/s/$username/"
readonly = true
}
button(classes ="ui teal right labeled icon button") {
i("copy icon")
+"Copy"
}
}
}
}
tr {
td { +"Password" }
td { i { +"Use your account password" } }
}
}
}
h2("ui header") { +"Account" }
button(classes ="ui button labeled icon change-password-button") {
i("lock icon")
+"Change password"
}
button(classes ="ui animated fade red button labeled icon delete-account-button") {
i("times icon")
div("visible content") { +"Close account" }
div("hidden content") { +"WIPE ALL DATA" }
}
h2("ui header") { +"Sync data" }
a(href = "/account/data.zip", target = "_blank") {
button(classes = "ui button labeled icon download-button") {
i("download icon")
+"Download data"
}
}
button(classes ="ui red button labeled icon clear-data-button") {
i("trash icon")
+"Clear data"
}
}
div("ui modal delete-account-modal") {
div("header") { +"Delete account?" }
div("content") {
+"Closing your account will remove it completely from our servers. "
b { +"All data in your account will be permanently deleted. " }
+"You will no longer be able to sync with your account."
}
div("actions") {
button(classes ="ui deny button") {
+"Cancel"
}
button(classes ="ui approve red button right icon labeled delete-account-modal-button") {
+"Close account"
i("times icon")
}
}
}
div("ui tiny modal clear-data-modal") {
div("header") { +"Clear all sync data?" }
div("content") {
+"All your sync data will be permanently deleted from our servers."
}
div("actions") {
button(classes ="ui deny button") {
+"Cancel"
}
button(classes ="ui approve red button right icon labeled clear-data-modal-button") {
+"Clear sync data"
i("trash icon")
}
}
}
div("ui tiny modal change-password-modal") {
div("header") { +"Change password" }
div("content") {
form(classes = "ui form") {
id="change-password-form"
div("field") {
label { +"New password" }
input(InputType.password, name = "cp-password")
}
div("field") {
label { +"Confirm new password" }
input(InputType.password, name = "cp-confirm-password")
}
div("ui error message")
}
}
div("actions") {
button(classes ="ui deny button") {
+"Cancel"
}
button(classes ="ui submit green button right icon labeled change-password-form-submit-button") {
form = "change-password-form"
+"Change password"
i("check icon")
}
}
}
div("ui page dimmer loading-dimmer") {
div("ui text huge loader") {
+"Loading..."
}
}
div("ui tiny modal complete-modal") {
div("header")
div("content")
div("actions") {
button(classes ="ui approve button") {
+"Close"
}
}
}
script {
unsafe {
//language=js
raw("""
window.onload = function() {
let sUrlBtn = $(".server-url");
sUrlBtn.find("button").click(function() {
sUrlBtn.find("input").select();
document.execCommand("copy");
});
$(".delete-account-button").click(function() {
$(".delete-account-modal").modal('show');
});
$(".clear-data-button").click(function() {
$(".clear-data-modal").modal('show');
});
$(".delete-account-modal-button").click(function() {
apiCall({
text: "Closing account...",
okHeader: "Account closed",
okText: "Your account has been deleted.",
failHeader: "Failed to close account",
failText: "An unknown error occurred and we could not close your account!",
okModalHide: logout
}, {
action: 'close account',
method: 'GET'
});
});
$(".clear-data-modal-button").click(function() {
apiCall({
text: "Clearing sync data...",
okHeader: "Sync data cleared",
okText: "Successfully cleared all sync data!",
failHeader: "Failed to clear sync data",
failText: "An unknown error occurred and the sync data could not be cleared!"
}, {
action: 'clear data',
method: 'GET'
});
});
function changePw() {
apiCall({
text: "Changing password...",
okHeader: "Password changed",
okText: "Successfully changed your password!",
failHeader: "Failed to change password",
failText: "An unknown error occurred and your password could not be changed!"
}, {
action: 'change password',
method: 'POST',
data: {
password: $("[name=cp-password]").val()
}
});
}
let loadingDimmer = $(".loading-dimmer").dimmer({ closable: false });
let loadingDimmerText = loadingDimmer.find(".text");
let completeModal = $('.complete-modal');
let completeModalHideListener = null;
completeModal.modal({ onHidden: function() {
if(completeModalHideListener != null)
completeModalHideListener();
}
});
function showLoadingDimmer(text) {
loadingDimmer.dimmer('show');
loadingDimmerText.text(text);
}
function hideLoadingDimmer() {
loadingDimmer.dimmer('hide');
}
function apiCall(args, obj) {
showLoadingDimmer(args.text);
obj.on = 'now';
obj.onComplete = function() {
hideLoadingDimmer();
};
obj.onSuccess = function() {
completeModal.find('.header').text(args.okHeader);
completeModal.find('.content').text(args.okText);
completeModal.modal('show');
completeModalHideListener = args.okModalHide;
if(args.onOk != null)
args.onOk();
};
obj.onFailure = function() {
completeModal.find('.header').text(args.failHeader);
completeModal.find('.content').text(args.failText);
completeModal.modal('show');
completeModalHideListener = args.failModalHide;
if(args.onFail != null)
args.onFail();
};
$('<div></div>').api(obj);
}
let changePasswordForm = $('#change-password-form');
let changePasswordModal = $(".change-password-modal");
changePasswordForm.form({
fields: {
password: {
identifier: 'cp-password',
rules: [
{
type: 'empty',
prompt: 'Please enter a new password'
}
]
},
confirmPassword: {
identifier: 'cp-confirm-password',
rules: [
{
type: 'empty',
prompt: 'Please re-enter your new password'
},
{
type: 'match[cp-password]',
prompt: 'Your confirmed new password is different from your new password! Please re-type your passwords.'
}
]
}
},
onSuccess: function() {
changePw();
changePasswordModal.modal('hide');
return false;
}
});
$(".change-password-button").click(function() {
changePasswordModal.modal('show');
changePasswordForm.form('clear');
changePasswordForm.removeClass('error');
});
$('.change-password-form-submit-button').click(function(e) {
e.preventDefault();
changePasswordForm.form('validate form');
});
};
""")
}
}
}
}
// language=html
/* return """
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Account Panel</title>
</head>
<body>
<p>Welcome back $username!</p>
<br>
<p><b>Sync Credentials:</b></p>
<p><b>URL:</b> <span style='font-family: monospace;'>${syncConfig.baseUrl}/s/$username/</span></p>
<p><b>Password:</b> <i>Use your account password</i></p>
<br>
<p>Delete all sync data:</p>
${action("Clear sync data", "/account/clear-data")}
<p>Change your password:</p>
${action("Change password", "/account/change-password")}
</body>
</html>
"""*/
// }
} | apache-2.0 | 732afc7a885475a7e575488998e4098a | 32.594286 | 129 | 0.460662 | 5.011509 | false | false | false | false |
joaomneto/TitanCompanion | src/main/java/pt/joaomneto/titancompanion/adventurecreation/impl/TWOFMAdventureCreation.kt | 1 | 1641 | package pt.joaomneto.titancompanion.adventurecreation.impl
import java.io.BufferedWriter
import java.io.IOException
import pt.joaomneto.titancompanion.R
import pt.joaomneto.titancompanion.adventurecreation.AdventureCreation
import pt.joaomneto.titancompanion.adventurecreation.impl.fragments.PotionsFragment
import pt.joaomneto.titancompanion.adventurecreation.impl.fragments.VitalStatisticsFragment
import pt.joaomneto.titancompanion.util.AdventureFragmentRunner
open class TWOFMAdventureCreation(
override val fragmentConfiguration: Array<AdventureFragmentRunner> = DEFAULT_FRAGMENTS
) : AdventureCreation(
fragmentConfiguration
) {
companion object {
val DEFAULT_FRAGMENTS = arrayOf(
AdventureFragmentRunner(
R.string.title_adventure_creation_vitalstats,
VitalStatisticsFragment::class
),
AdventureFragmentRunner(
R.string.title_adventure_creation_potions,
PotionsFragment::class
)
)
}
var potion = -1
var potionDoses = -1
@Throws(IOException::class)
override fun storeAdventureSpecificValuesInFile(bw: BufferedWriter) {
bw.write("standardPotion=" + potion + "\n")
bw.write("standardPotionValue=" + potionDoses + "\n")
bw.write("provisions=10\n")
bw.write("provisionsValue=4\n")
bw.write("gold=0\n")
}
override fun validateCreationSpecificParameters(): String? {
val sb = StringBuilder()
if (this.potion < 0) {
sb.append(getString(R.string.potion))
}
return sb.toString()
}
}
| lgpl-3.0 | adbacd77b60e0f691d0c4c8f96292561 | 32.489796 | 91 | 0.689214 | 4.756522 | false | false | false | false |
vicpinm/KPresenterAdapter | library/src/main/java/com/vicpin/kpresenteradapter/ViewHolderPresenter.kt | 1 | 1700 | package com.vicpin.kpresenteradapter
import android.widget.AbsListView
import com.vicpinm.autosubscription.Unsubscriber
import java.util.concurrent.atomic.AtomicInteger
/**
* Created by Victor on 01/11/2016.
*/
abstract class ViewHolderPresenter<Data : Any, PresenterView: Any> {
companion object{
val presenterIdsGenerator: AtomicInteger = AtomicInteger()
}
var view: PresenterView? = null
lateinit var data: Data
lateinit var dataCollection: List<Data>
internal var onDeleteListener: (() -> Unit)? = null
internal var refreshViewsListener: (() -> Unit)? = null
var scrollState: Int = AbsListView.OnScrollListener.SCROLL_STATE_IDLE
val presenterId: Int by lazy { presenterIdsGenerator.andIncrement }
var visible: Boolean = false
fun setPresenterView(view: Any){
this.view = view as? PresenterView
}
/**
* Called when this viewholder is binded to the current data item
*/
abstract fun onCreate()
/**
* Called when the view becomes visible to user
*/
open fun onAttach() {
}
/**
* Called when the view is out of the screen
*/
open fun onDetach() {
}
open fun onScrollStoped() {
}
fun onPreDestroy(){
Unsubscriber.unlink(this)
onDestroy()
}
/**
* Called when the view is recycled and is ready to be reused
*/
open fun onDestroy(){}
fun deleteItemFromCollection() {
onDeleteListener?.invoke()
}
open fun onShowed() {
}
fun refreshVisibleViewHolders() {
refreshViewsListener?.invoke()
}
} | apache-2.0 | b043b8273e7110e875a7751601a85c64 | 20.103896 | 73 | 0.62 | 4.709141 | false | false | false | false |
bhubie/Expander | app/src/main/kotlin/com/wanderfar/expander/Models/MacroStore.kt | 1 | 2679 | /*
* Expander: Text Expansion Application
* Copyright (C) 2016 Brett Huber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.wanderfar.expander.Models
import io.paperdb.Paper
object MacroStore {
fun saveMacro(macro: Macro){
Paper.book("Macros").write(macro.name, macro)
setMacroStoreUpdatedFlag(true)
}
fun hasStoreBeenUpdated(): Boolean{
return Paper.book().read("macroStoreUpdated", false)
}
fun setMacroStoreUpdatedFlag(flag: Boolean) {
Paper.book().write("macroStoreUpdated", flag)
}
fun getMacroKeys() : MutableList<String>{
return Paper.book("Macros").allKeys
}
fun getMacros() : MutableList<Macro>{
val macroList = mutableListOf<Macro>()
val keys = Paper.book("Macros").allKeys
keys.mapTo(macroList) { //val macro = macroDB.getObject(item, Macro::class.java)
Paper.book("Macros").read<Macro>(it)
}
return macroList
}
fun deleteMacro(name : String){
//Paper.init(Expander.context)
Paper.book("Macros").delete(name)
setMacroStoreUpdatedFlag(true)
}
fun getMacro(macroToLoad: String) : Macro? {
val macro = Paper.book("Macros").read<Macro>(macroToLoad)
return macro
}
fun hasMacroChanged(macroToCheck: Macro, originalName: String): Boolean{
val loadedMacro = Paper.book("Macros").read<Macro>(originalName)
println(loadedMacro.areObjectMemberEqual(macroToCheck))
return loadedMacro.areObjectMemberEqual(macroToCheck).not()
}
fun Macro.areObjectMemberEqual(macroToCheck : Macro): Boolean{
return this.name == macroToCheck.name && this.phrase == macroToCheck.phrase
&& this.description.equals(macroToCheck.description) && this.isCaseSensitive == macroToCheck.isCaseSensitive
&& this.expandWhenSetting == macroToCheck.expandWhenSetting && this.macroPattern == macroToCheck.macroPattern
&& this.expandWithinWords == macroToCheck.expandWithinWords
}
} | gpl-3.0 | 1da0a58a2da21c715f2f3a70284ad10d | 28.450549 | 125 | 0.681598 | 4.259141 | false | false | false | false |
getsentry/raven-java | sentry-android-timber/src/test/java/io/sentry/android/timber/SentryTimberTreeTest.kt | 1 | 6306 | package io.sentry.android.timber
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.check
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.never
import com.nhaarman.mockitokotlin2.verify
import io.sentry.Breadcrumb
import io.sentry.IHub
import io.sentry.SentryLevel
import io.sentry.getExc
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue
import timber.log.Timber
class SentryTimberTreeTest {
private class Fixture {
val hub = mock<IHub>()
fun getSut(
minEventLevel: SentryLevel = SentryLevel.ERROR,
minBreadcrumbLevel: SentryLevel = SentryLevel.INFO
): SentryTimberTree {
return SentryTimberTree(hub, minEventLevel, minBreadcrumbLevel)
}
}
private val fixture = Fixture()
@BeforeTest
fun beforeTest() {
Timber.uprootAll()
}
@Test
fun `Tree captures an event if min level is equal`() {
val sut = fixture.getSut()
sut.e(Throwable())
verify(fixture.hub).captureEvent(any())
}
@Test
fun `Tree captures an event if min level is higher`() {
val sut = fixture.getSut()
sut.wtf(Throwable())
verify(fixture.hub).captureEvent(any())
}
@Test
fun `Tree won't capture an event if min level is lower`() {
val sut = fixture.getSut()
sut.d(Throwable())
verify(fixture.hub, never()).captureEvent(any())
}
@Test
fun `Tree captures debug level event`() {
val sut = fixture.getSut(SentryLevel.DEBUG)
sut.d(Throwable())
verify(fixture.hub).captureEvent(check {
assertEquals(SentryLevel.DEBUG, it.level)
})
}
@Test
fun `Tree captures info level event`() {
val sut = fixture.getSut(SentryLevel.DEBUG)
sut.i(Throwable())
verify(fixture.hub).captureEvent(check {
assertEquals(SentryLevel.INFO, it.level)
})
}
@Test
fun `Tree captures warning level event`() {
val sut = fixture.getSut(SentryLevel.DEBUG)
sut.w(Throwable())
verify(fixture.hub).captureEvent(check {
assertEquals(SentryLevel.WARNING, it.level)
})
}
@Test
fun `Tree captures error level event`() {
val sut = fixture.getSut(SentryLevel.DEBUG)
sut.e(Throwable())
verify(fixture.hub).captureEvent(check {
assertEquals(SentryLevel.ERROR, it.level)
})
}
@Test
fun `Tree captures fatal level event`() {
val sut = fixture.getSut(SentryLevel.DEBUG)
sut.wtf(Throwable())
verify(fixture.hub).captureEvent(check {
assertEquals(SentryLevel.FATAL, it.level)
})
}
@Test
fun `Tree captures unknown as debug level event`() {
val sut = fixture.getSut(SentryLevel.DEBUG)
sut.log(15, Throwable())
verify(fixture.hub).captureEvent(check {
assertEquals(SentryLevel.DEBUG, it.level)
})
}
@Test
fun `Tree captures an event with an exception`() {
val sut = fixture.getSut()
val throwable = Throwable()
sut.e(throwable)
verify(fixture.hub).captureEvent(check {
assertEquals(throwable, it.getExc())
})
}
@Test
fun `Tree captures an event without an exception`() {
val sut = fixture.getSut()
sut.e("message")
verify(fixture.hub).captureEvent(check {
assertNull(it.getExc())
})
}
@Test
fun `Tree captures an event and sets Timber as a logger`() {
val sut = fixture.getSut()
sut.e("message")
verify(fixture.hub).captureEvent(check {
assertEquals("Timber", it.logger)
})
}
@Test
fun `Tree captures an event with TimberTag tag`() {
val sut = fixture.getSut()
Timber.plant(sut)
// only available thru static class
Timber.tag("tag")
Timber.e("message")
verify(fixture.hub).captureEvent(check {
assertEquals("tag", it.getTag("TimberTag"))
})
}
@Test
fun `Tree captures an event without TimberTag tag`() {
val sut = fixture.getSut()
Timber.plant(sut)
Timber.e("message")
verify(fixture.hub).captureEvent(check {
assertNull(it.getTag("TimberTag"))
})
}
@Test
fun `Tree captures an event with given message`() {
val sut = fixture.getSut()
sut.e("message")
verify(fixture.hub).captureEvent(check {
assertNotNull(it.message) { message ->
assertEquals("message", message.formatted)
}
})
}
@Test
fun `Tree adds a breadcrumb if min level is equal`() {
val sut = fixture.getSut()
sut.i(Throwable("test"))
verify(fixture.hub).addBreadcrumb(any<Breadcrumb>())
}
@Test
fun `Tree adds a breadcrumb if min level is higher`() {
val sut = fixture.getSut()
sut.e(Throwable("test"))
verify(fixture.hub).addBreadcrumb(any<Breadcrumb>())
}
@Test
fun `Tree won't add a breadcrumb if min level is lower`() {
val sut = fixture.getSut(minBreadcrumbLevel = SentryLevel.ERROR)
sut.i(Throwable("test"))
verify(fixture.hub, never()).addBreadcrumb(any<Breadcrumb>())
}
@Test
fun `Tree adds a breadcrumb with given level`() {
val sut = fixture.getSut()
sut.e(Throwable("test"))
verify(fixture.hub).addBreadcrumb(check<Breadcrumb> {
assertEquals(SentryLevel.ERROR, it.level)
})
}
@Test
fun `Tree adds a breadcrumb with Timber category`() {
val sut = fixture.getSut()
sut.e(Throwable("test"))
verify(fixture.hub).addBreadcrumb(check<Breadcrumb> {
assertEquals("Timber", it.category)
})
}
@Test
fun `Tree adds a breadcrumb with exception message`() {
val sut = fixture.getSut()
sut.e(Throwable("test"))
verify(fixture.hub).addBreadcrumb(check<Breadcrumb> {
assertTrue(it.message!!.contains("test"))
})
}
}
| bsd-3-clause | a4c70f57a53bc3dc54303b2df5480795 | 27.405405 | 75 | 0.603076 | 4.337001 | false | true | false | false |
Suomaa/androidLearning | FCMKotlin/app/src/main/java/fi/lasicaine/fcm/MyFirebaseMessagingService.kt | 1 | 2901 | package fi.lasicaine.fcm
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.media.RingtoneManager
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import java.util.*
class MyFirebaseMessagingService : FirebaseMessagingService() {
private val TAG = "MyFirebaseToken"
private lateinit var notificationManager: NotificationManager
private val ADMIN_CHANNEL_ID = "Android4Dev"
override fun onNewToken(token: String?) {
super.onNewToken(token)
Log.i(TAG, token!!)
}
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
remoteMessage?.let { message ->
Log.i(TAG, message.getData().get("message")!!)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//Setting up Notification channels for android O and above
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
setupNotificationChannels()
}
val notificationId = Random().nextInt(60000)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher) //a resource for your custom small icon
.setContentTitle(message.data["title"]) //the "title" value you sent in your notification
.setContentText(message.data["message"]) //ditto
.setAutoCancel(true) //dismisses the notification on click
.setSound(defaultSoundUri)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId /* ID of notification */, notificationBuilder.build())
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private fun setupNotificationChannels() {
val adminChannelName = getString(R.string.notifications_admin_channel_name)
val adminChannelDescription = getString(R.string.notifications_admin_channel_description)
val adminChannel: NotificationChannel
adminChannel = NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW)
adminChannel.description = adminChannelDescription
adminChannel.enableLights(true)
adminChannel.lightColor = Color.RED
adminChannel.enableVibration(true)
notificationManager.createNotificationChannel(adminChannel)
}
} | apache-2.0 | 4fc332c54e0e868166cdc3c68f2188c2 | 41.057971 | 114 | 0.724922 | 5.255435 | false | false | false | false |
orgzly/orgzly-android | app/src/main/java/com/orgzly/android/ui/views/richtext/RichTextEdit.kt | 1 | 5742 | package com.orgzly.android.ui.views.richtext
import android.content.Context
import android.graphics.Rect
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.view.ancestors
import androidx.core.widget.NestedScrollView
import com.orgzly.BuildConfig
import com.orgzly.android.ui.util.KeyboardUtils
import com.orgzly.android.util.LogUtils
class RichTextEdit : AppCompatEditText {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val userEditingTextWatcher: TextWatcher = RichTextEditWatcher()
fun activate(charOffset: Int) {
visibility = View.VISIBLE
// Position the cursor and open the keyboard
if (charOffset in 0..(text?.length ?: 0)) {
performClick()
setSelection(charOffset)
KeyboardUtils.openSoftKeyboard(this) {
scrollForBetterCursorPosition(charOffset)
}
}
addTextChangedListener(userEditingTextWatcher)
}
// TODO: Handle closed drawers (and such)
private fun scrollForBetterCursorPosition(charOffset: Int) {
val scrollView = ancestors.firstOrNull { view -> view is NestedScrollView } as? NestedScrollView
if (scrollView != null) {
post {
val richText = parent as RichText
val line = layout.getLineForOffset(charOffset)
val baseline = layout.getLineBaseline(line)
val ascent = layout.getLineAscent(line)
val cursorY = richText.top + (baseline + ascent)
val visibleHeight = Rect().let { rect ->
scrollView.getDrawingRect(rect)
rect.bottom - rect.top
}
val scrollTopY = scrollView.scrollY
val scroll75pY = scrollTopY + (visibleHeight*3/4)
// Scroll unless cursor is already in the top part of the visible rect
val scrollTo = if (cursorY < scrollTopY) { // Too high
cursorY
} else if (cursorY > scroll75pY) { // Too low
cursorY - (visibleHeight*3/4)
} else {
-1
}
if (scrollTo != -1) {
scrollView.smoothScrollTo(0, scrollTo)
}
if (BuildConfig.LOG_DEBUG) {
// fun pad(n: Any) = "$n".padEnd(5)
//
// LogUtils.d(TAG, pad(y), "this.y")
// LogUtils.d(TAG, pad(top), "this.top")
// LogUtils.d(TAG, pad(bottom), "this.bottom")
// LogUtils.d(TAG, pad(richText.top), "richText.top")
// LogUtils.d(TAG, pad(richText.bottom), "richText.bottom")
// LogUtils.d(TAG, pad(visibleHeight), "visibleHeight")
// LogUtils.d(TAG, pad(scrollTopY), "scrollTopY")
// LogUtils.d(TAG, pad(scroll75pY), "scroll75pY")
// LogUtils.d(TAG, pad(cursorY), "cursorY")
// Rect().let { rect -> getLocalVisibleRect(rect)
// LogUtils.d(TAG, pad(rect.top), "getLocalVisibleRect.top")
// LogUtils.d(TAG, pad(rect.bottom), "getLocalVisibleRect.bottom")
// }
// Rect().let { rect -> getWindowVisibleDisplayFrame(rect)
// LogUtils.d(TAG, pad(rect.top), "getWindowVisibleDisplayFrame.top")
// LogUtils.d(TAG, pad(rect.bottom), "getWindowVisibleDisplayFrame.bottom")
// }
// Rect().let { rect -> getGlobalVisibleRect(rect)
// LogUtils.d(TAG, pad(rect.top), "getGlobalVisibleRect.top")
// LogUtils.d(TAG, pad(rect.bottom), "getGlobalVisibleRect.bottom")
// }
// Rect().let { rect -> scrollView.getDrawingRect(rect)
// LogUtils.d(TAG, pad(rect.top), "scrollView.getDrawingRect.top")
// LogUtils.d(TAG, pad(rect.bottom), "scrollView.getDrawingRect.bottom")
// }
// IntArray(2).let { arr -> scrollView.getLocationInWindow(arr)
// LogUtils.d(TAG, pad(arr[0]), "scrollView.getLocationInWindow.x")
// LogUtils.d(TAG, pad(arr[1]), "scrollView.getLocationInWindow.y")
// }
//
LogUtils.d(TAG, scrollTo)
}
}
}
}
fun deactivate() {
removeTextChangedListener(userEditingTextWatcher)
visibility = View.GONE
}
/* Clear the focus on back press before letting IME handle the event. */
override fun onKeyPreIme(keyCode: Int, event: KeyEvent?): Boolean {
if (BuildConfig.LOG_DEBUG) LogUtils.d(TAG, keyCode, event)
if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_UP) {
if (BuildConfig.LOG_DEBUG) LogUtils.d(TAG, "Clear focus before IME handling the event")
clearFocus()
}
return super.onKeyPreIme(keyCode, event)
}
// override fun onSelectionChanged(selStart: Int, selEnd: Int) {
// super.onSelectionChanged(selStart, selEnd)
// if (BuildConfig.LOG_DEBUG) LogUtils.d(TAG, "${selStart}-${selEnd}")
// }
companion object {
val TAG: String = RichTextEdit::class.java.name
}
} | gpl-3.0 | 675534c661d225c81181990f194ccf05 | 40.316547 | 112 | 0.569662 | 4.489445 | false | false | false | false |
RSDT/Japp | app/src/main/java/nl/rsdt/japp/application/fragments/JappPreferenceFragment.kt | 1 | 4421 | package nl.rsdt.japp.application.fragments
import android.os.Bundle
import android.preference.*
import android.view.View
import nl.rsdt.japp.BuildConfig
import nl.rsdt.japp.R
import nl.rsdt.japp.application.Japp
import nl.rsdt.japp.application.JappPreferences
import nl.rsdt.japp.jotial.data.structures.area348.AutoInzittendeInfo
import nl.rsdt.japp.jotial.data.structures.area348.HunterInfo
import nl.rsdt.japp.jotial.maps.deelgebied.Deelgebied
import nl.rsdt.japp.jotial.net.apis.AutoApi
import retrofit2.Call
import retrofit2.Response
import javax.security.auth.callback.Callback
/**
* @author Dingenis Sieger Sinke
* @version 1.0
* @since 10-7-2016
* Description...
*/
class JappPreferenceFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.release_preferences)
if (BuildConfig.DEBUG) {
addPreferencesFromResource(R.xml.debug_preferences)
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setupIconChange()
val screen = preferenceScreen
val map = screen.findPreference(JappPreferences.PREF_CAT_MAP) as PreferenceCategory
val type = ListPreference(this.activity)
if (JappPreferences.useOSM()) {
type.key = "pref_map_osm_source"
type.setTitle(R.string.pref_map_osm_source_title)
type.setSummary(R.string.pref_map_osm_source_sum)
type.setEntries(R.array.pref_map_osm_source_options)
type.setEntryValues(R.array.pref_map_osm_source_options_data)
type.setDefaultValue(getString(R.string.pref_map_osm_source_options_def))
} else {
type.key = "pref_map_type"
type.setTitle(R.string.pref_map_type_title)
type.setSummary(R.string.pref_map_type_sum)
type.setEntries(R.array.pref_map_type_options)
type.setEntryValues(R.array.pref_map_type_options_data)
type.setDefaultValue(getString(R.string.pref_map_type_options_def))
type.setDialogTitle(R.string.pref_map_type_dialog_title)
val style = ListPreference(this.activity)
style.key = "pref_map_style"
style.setTitle(R.string.pref_map_style_title)
style.setSummary(R.string.pref_map_style_sum)
style.setEntries(R.array.pref_map_style_options)
style.setEntryValues(R.array.pref_map_style_options_data)
style.setDefaultValue(getString(R.string.pref_map_style_options_def))
style.setDialogTitle(R.string.pref_map_style_dialog_title)
map.addPreference(style)
}
map.addPreference(type)
val preference = findPreference(JappPreferences.DEBUG_VERSION_NAME) as EditTextPreference
val version = BuildConfig.VERSION_NAME
preference.text = version
}
private fun setupIconChange() {
val preference = findPreference(JappPreferences.ACCOUNT_ICON)
preference.setIcon(HunterInfo.getAssociatedDrawable(JappPreferences.accountIcon, JappPreferences.taak.name))
preference.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, o ->
val value = Integer.valueOf(o as String)
val api = Japp.getApi(AutoApi::class.java)
api.getInfoById(JappPreferences.accountKey, JappPreferences.accountId).enqueue(object : retrofit2.Callback<AutoInzittendeInfo?>{
override fun onFailure(call: Call<AutoInzittendeInfo?>, t: Throwable) {
preference.setIcon(HunterInfo.getAssociatedDrawable(value, Deelgebied.Xray.name))
}
override fun onResponse(call: Call<AutoInzittendeInfo?>, response: Response<AutoInzittendeInfo?>) {
if (response.isSuccessful){
val dg = Deelgebied.parse(response.body()?.taak?:"X") ?: Deelgebied.Xray
preference.setIcon(HunterInfo.getAssociatedDrawable(value, dg.name))
}else{
preference.setIcon(HunterInfo.getAssociatedDrawable(value, Deelgebied.Xray.name))
}
}
})
true
}
}
companion object {
val TAG = "JappPreferenceFragment"
}
}
| apache-2.0 | 4b54e0091f4f99c25545a9507460bcd0 | 42.772277 | 140 | 0.672472 | 4.321603 | false | false | false | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.